Submission #755936

# Submission time Handle Problem Language Result Execution time Memory
755936 2023-06-10T18:01:10 Z vjudge1 Password (RMI18_password) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <assert.h>
#include <bits/stdc++.h>
using namespace std;

#define DEBUG 0
#define MAX_QUERIES 50000
#define MAX_N 5000
#define MAX_SIGMA 26

static string password;
static int n, s, numQueries;
static stringstream msg;

static void end(int success) {
  cout << (success ? "SUCCESS! " : "ERROR: ") << msg.str() << endl;
  exit(0);
}

int query(string q) {
  if (++numQueries > MAX_QUERIES) {
    msg << "Could not guess the password with " << MAX_QUERIES << " questions.";
    end(0);
  }

  int len = q.size();

  // validation
  if (len < 1 || len > n) {
    msg << "Query '" << q << "' should have length between 1 and "
        << n << " characters.";
    end(0);
  }

  for (int i = 0; i < len; i++) {
    if (q[i] < 'a' || q[i] >= 'a' + s) {
      msg << "Illegal character '" << q[i] << "' found.";
      end(0);
    }
  }

  // while possible, advance one character in q and find its next
  // occurrence in password
  int i = 0, j = 0, plen = password.size();
  while (i < plen && j < len) {
    while ((i < plen) && (password[i] != q[j])) {
      i++;
    }
    if (i < plen) {  
      i++;
      j++;
    }
  }

  if (DEBUG) {
    cout << "Question #" << numQueries << " [" << q << "] answer " << j << endl;
  }
  return j;
}
int query(string q);
string guess(int n, int s){
    string res="";
    int sum=0;
    int cur=0;
    while(cur<n)
    {
        bool test=false;
        for (int i = 0; i < s; ++i)
        {
            string t="";
            char x=char(i+'a');
            for (int i = 0; i < cur; ++i)
            {
                t.push_back(res[i]);
            }t.push_back(x);
            for (int i = cur; i < res.size(); ++i)
            {
                t.push_back(res[i]);
            }
            if (t.size()>n) break;
            if (query(t)>sum){
                sum++;
                test=true;
                res="";
                for (int i = 0; i < t.size(); ++i)
                {
                    res.push_back(t[i]);
                }
                break;
            }
        }
        if(!test) cur++;
    }
    //cout<< sum<<endl;
    return res;
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
  //ifstream f("password.in");
  cin >> n >> s >> password;
    //f.close();
  assert(n && s && !password.empty());

  string answer = guess(n, s);
  if (DEBUG) {
    cout << "Your answer: [" << answer << "]\n";
  }

  if (answer.compare(password)) {
    msg << "Your answer was [" << answer
        << "] which does not match the password [" << password << "].";
    end(0);
  } else {
    msg << "You guessed the password with " << numQueries << " queries.";
    end(1);
  }

    return 0;
}

Compilation message

password.cpp: In function 'std::string guess(int, int)':
password.cpp:81:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |             for (int i = cur; i < res.size(); ++i)
      |                               ~~^~~~~~~~~~~~
password.cpp:85:25: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   85 |             if (t.size()>n) break;
      |                 ~~~~~~~~^~
password.cpp:90:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |                 for (int i = 0; i < t.size(); ++i)
      |                                 ~~^~~~~~~~~~
password.cpp: In function 'int main()':
password.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |         freopen("input.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
password.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |         freopen("output.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/cc24iblI.o: in function `query(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
grader.cpp:(.text+0x80): multiple definition of `query(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'; /tmp/ccCD9YUF.o:password.cpp:(.text+0x1b0): first defined here
/usr/bin/ld: /tmp/cc24iblI.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccCD9YUF.o:password.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status