This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#ifdef LOCAL
#include <string>
std::string guess_sequence(int N);
int press(std::string p);
#else
#include "combo.h"
#endif
#ifdef LOCAL
#include "template\debug.hpp"
#else
#define dbg(...) ;
#define timer(...) ;
#endif
std::string guess_sequence(int N) {
auto find_first_char = [&]() -> char {
int res = press("AB");
if (res == 0) {
res = press("X");
if (res == 0) {
return 'Y';
} else {
return 'X';
}
} else {
res = press("A");
if (res == 0) {
return 'B';
} else {
return 'A';
}
}
};
char first_char = find_first_char();
std::string char_set = "ABXY";
char_set.erase(std::find(char_set.begin(), char_set.end(), first_char));
std::vector<std::string> candidates(3, std::string(1, first_char));
if (N == 1) {
return candidates[0];
}
for (int i = 0; i < 3; i++) {
candidates[i] += char_set[i];
}
auto check_type = [&](int p) -> bool {
for (int i = 1; i < 3; i++) {
if (candidates[i][p] != candidates[0][p]) {
if (i == 2) return false;
// i == 1
if (candidates[0][p] == candidates[2][p]) {
// this guy is fraud
std::swap(candidates[1], candidates[2]);
} else {
// which means cand[0] is fraud
std::swap(candidates[0], candidates[2]);
}
return false;
}
}
return true;
};
int matched = 1; // this is the match score
for (int i = 1; i < N - 1; i++) { // N - 2 queries
dbg(i, matched);
for (int j = 0; j < 3; j++) dbg(candidates[j]);
if (check_type(i - 1)) {
std::string qstring;
qstring += candidates[0] + char_set[0];
qstring += candidates[0] + char_set[1];
qstring += candidates[1] + char_set[0];
int res = press(qstring) - matched;
if (res == 0) {
candidates[0] = candidates[2];
candidates[1] = candidates[2];
for (int j = 0; j < 3; j++) candidates[j] += char_set[j];
// this means we match another
matched++;
} else if (res == 1) {
candidates[2] = candidates[1];
candidates[0] += char_set[2];
candidates[1] += char_set[1];
candidates[2] += char_set[2];
} else {
candidates[2] = candidates[0];
candidates[0] += char_set[0];
candidates[1] += char_set[0];
candidates[2] += char_set[1];
}
} else {
std::string qstring;
qstring += candidates[1] + char_set[0];
qstring += candidates[1] + char_set[1];
qstring += candidates[1] + char_set[2];
int res = press(qstring) - matched;
if (res == 0) {
candidates[0] = candidates[2];
candidates[1] = candidates[2];
} else if (res == 1) {
candidates[1] = candidates[0];
candidates[2] = candidates[0];
} else {
candidates[0] = candidates[1];
candidates[2] = candidates[1];
}
matched += 2;
for (int j = 0; j < 3; j++) candidates[j] += char_set[j];
}
}
for (int j = 0; j < 2; j++) {
if (press(candidates[j]) == N) return candidates[j];
}
return candidates[2];
}
#ifdef LOCAL
#define nl '\n'
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include "combo.h"
namespace {
constexpr int MAX_N = 2000;
constexpr int MAX_NUM_MOVES = 8000;
int N;
std::string S;
int num_moves;
void wrong_answer(const char *MSG) {
printf("Wrong Answer: %s\n", MSG);
exit(0);
}
} // namespace
int press(std::string p) {
dbg(p);
if (++num_moves > MAX_NUM_MOVES) {
wrong_answer("too many moves");
}
int len = p.length();
if (len > 4 * N) {
wrong_answer("invalid press");
}
for (int i = 0; i < len; ++i) {
if (p[i] != 'A' && p[i] != 'B' && p[i] != 'X' && p[i] != 'Y') {
wrong_answer("invalid press");
}
}
int coins = 0;
for (int i = 0, j = 0; i < len; ++i) {
if (j < N && S[j] == p[i]) {
++j;
} else if (S[0] == p[i]) {
j = 1;
} else {
j = 0;
}
coins = std::max(coins, j);
}
dbg(coins);
return coins;
}
int main() {
char buffer[MAX_N + 1];
if (scanf("%s", buffer) != 1) {
fprintf(stderr, "Error while reading input\n");
exit(1);
}
S = buffer;
N = S.length();
num_moves = 0;
std::string answer = guess_sequence(N);
if (answer != S) {
wrong_answer("wrong guess");
exit(0);
}
printf("Accepted: %d\n", num_moves);
return 0;
}
#endif
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |