# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
726786 | Cookie | Combo (IOI18_combo) | C++14 | 0 ms | 0 KiB |
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>
using namespace std;
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#include <cstdio>
#include <cstdlib>
#include <vector>
//#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) {
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);
}
return coins;
}
const char c[4] = {'A', 'B', 'X', 'Y'};
string guess_sequence(int n){
string res = "";
char fir;
int firstid = -1, secondid = -1;
int a1 = press("AB");
if(a1 >= 1){
int a2 = press("A");
if(a2 > 0){
res += "A"; fir = 'A';
}else{
res += "B"; fir = 'B';
}
}else{
int a2 = press("X");
if(a2 > 0){
res += "X"; fir = 'X';
}else{
res += "Y"; fir = 'Y';
}
}
forr(i, 0, 4){
if(c[i] != fir){
if(firstid == -1)firstid = i;
else if(secondid == -1) secondid = i;
}
}
int thirdid;
if(fir == 'Y')thirdid = 2;
else thirdid = 3;
assert(firstid != secondid && secondid != thirdid);
for(int i = 1; i < n - 1; i++){
string guess = "";
guess += res; guess += c[firstid];
forr(j, 0, 3){
guess += res; guess += c[secondid];
if(!j)guess += c[firstid];
else if(j == 1)guess += c[secondid];
else guess += c[thirdid];
}
int ans = press(guess);
if(ans == i){
res += c[thirdid];
}else if(ans == i + 1){
res += c[firstid];
}else{
res += c[secondid];
}
}
string guess;
guess += res; guess += c[firstid]; guess += res; guess += c[secondid];
int ans = press(guess);
if(ans == n - 1){
res += c[thirdid];
}else{
guess = res + c[firstid];
ans = press(guess);
if(ans == n - 1){
res += c[secondid];
}else{
res += c[firstid];
}
}
return(res);
}
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;
}