이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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) {
// Guessing the first character takes 2 moves
// Now we got N moves left
// each move, we have to eliminate 2 possibilities
// AA, AB, AC, BA, BB, BC, CA, CB, CC // these are the possiblities
// identify one in 2 moves
// N = 3
// do something to init in 4 moves
// 4 * 3 = 12 possibilities
// take 2 -> solve for first one -> A
// there are 9 possibilites: 3 more queries
// - AB -> ABB
// ABC
// ABD
// - AC -> ACB
// ACC
// ACD
// - AD -> ADB
// ADC
// ADD
// ADC is our answer
// we have to do this in 3 moves -> what can we do?
// we ask ABBABCACBACC -> ask for 4
// 2 -> in here -> we got 4 branch
// ABBABCACC -> 0 -> remove 6 branch -> 3 branch left
// -> remain 3 branch
// it is the answer -> 3 branch
// every time we can remove 6 branch
// // >= 1 -> not be 2
// 1. ABBB
// 1. ABBC -> here -> 3 -> original
// 1. ABBD
// -------
// 2. ABCB
// 2. ABCC
// 2. ABCD -> 0, 1, 3 -> ok it works -> it is definitely one of the thing up here
// -------
// 3. ACCB
// 3. ACCC
// 3. ACCD -> original
// ACC
std::string S;
auto find_first_char = [&]() -> char {
int res = press("AB");
if (res == 0) {
res = press("XY");
if (res == 0) {
return 'Y';
} else {
return 'X';
}
} else {
res = press("A");
if (res == 0) {
return 'B';
} else {
return 'A';
}
}
};
std::string char_set = "ABXY";
char first_char = find_first_char();
dbg(first_char);
char_set.erase(std::find(char_set.begin(), char_set.end(), first_char));
std::vector<std::string> candidates(3, std::string(1, first_char));
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];
// we didn't match
} 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];
}
}
dbg(candidates[0]);
dbg(candidates[1]);
dbg(candidates[2]);
// we can do it with 2 more queries
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... |