#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
using namespace std;
#define int long long
#define pb push_back
#define nl '\n'
#define fore(i, y) for(int i = 0; i < y; i++)
#define forr(i, x, y) for(int i = x;i<=y;i++)
#define forn(i, y, x) for(int i = y; i >= x; i--)
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) { /* found a match */
i++;
j++;
}
}
if (DEBUG) {
cout << "Question #" << numQueries << " [" << q << "] answer " << j << endl;
}
return j;
}
string comb(string a , string b)
{
if(b.empty())
return a;
if(a.empty())
return b;
string cur = "";
vector<string> suf;
forn(i , (int)b.size() - 1 , 0)
{
cur+=b[i];
string rcur = cur;
reverse(rcur.begin() , rcur.end());
suf.pb(rcur);
}
reverse(suf.begin() , suf.end());
suf.pb("");
string res = "";
int ptr = 0;
forr(i , 0 , (int)b.size())
{
if(ptr == (int)a.size())
{
if(i < (int)b.size())
res+=b[i];
continue;
}
string beg = "";
beg+=a[ptr];
string new_res = res + beg + suf[i];
while(query(new_res) == (int)new_res.size())
{
res = res + beg;
ptr++;
if(ptr == (int)a.size())
break;
beg = "";
beg+=a[ptr];
new_res = res + beg + suf[i];
}
if(i < (int)b.size())
res+=b[i];
}
return res;
}
string get(int l , int r , int &N)
{
if(l == r)
{
string str = string(N , (char)('a' + l));
int L = query(str);
return string(L , (char)('a' + l));
}
int mid = (l + r)/2;
string a = get(l , mid , N) , b = get(mid + 1 , r , N);
return comb(a , b);
}
string guess(int N, int S)
{
return get(0 , S - 1 , N);
}
signed main() {
ifstream f("password.in");
f >> 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
/usr/bin/ld: /tmp/ccguCxAI.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/ccJBYOJF.o:password.cpp:(.text+0x1b0): first defined here
/usr/bin/ld: /tmp/ccguCxAI.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccJBYOJF.o:password.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccguCxAI.o: in function `main':
grader.cpp:(.text.startup+0x66): undefined reference to `guess[abi:cxx11](int, int)'
collect2: error: ld returned 1 exit status