-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathioi18p1.cpp
More file actions
154 lines (149 loc) · 4.31 KB
/
Copy pathioi18p1.cpp
File metadata and controls
154 lines (149 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
my code is quite repetitive, but the idea is simple.
guess a letter for the first letter of the sequence. if the
grader returns 0, then that letter is not the first letter of
the sequence. if the same happens when two other letters are
guessed, then we know that the fourth letter is the starting
letter of the sequence.
now for the remaining positions in the string, we only have to
choose from 3 letters. if we use the same logic as above, the
number of queries in the worst case will be 2N + 1, which
will be enough to fetch 25 points.
for 97 points, we need to find each letter (except the first and
last letter) using one query. take advantage of the fact that we
are allowed to pass a large(4N) string to the press() function.
let's assume that the first letter of the sequence is 'A'. This
means that 'A' will never be repeated in the string again. let s
be the string that we have determined so far.
to determine the ith letter, pass a query like this:
s + 'B' + s + 'XB' + s + 'XX' + s + 'XY'
if the ith letter is 'B', the the press() function will return
s.size() + 1. if the ith letter is 'X' then the pres() function
will return s.size() + 2. but if it returns s.size(), the it will
return 'Y'.
*/
#include <bits/stdc++.h>
using namespace std;
// string real_S;
// int press(string s) {
// for (int i = 1; i <= real_S.size(); i++) {
// string tp = real_S.substr(0, i);
// if (s.find(tp) == string::npos)
// return i-1;
// }
// return real_S.size();
// }
char add_letter(string S, int res) {
if (S[0] == 'A') {
if (res == S.size() + 1)
return 'B';
else if (res == S.size() + 2)
return 'X';
else
return 'Y';
} else if (S[0] == 'B') {
if (res == S.size() + 1)
return 'A';
else if (res == S.size() + 2)
return 'X';
else
return 'Y';
} else if (S[0] == 'X') {
if (res == S.size() + 1)
return 'A';
else if (res == S.size() + 2)
return 'B';
else
return 'Y';
} else {
if (res == S.size() + 1)
return 'A';
else if (res == S.size() + 2)
return 'B';
else
return 'X';
}
}
string guess_sequence(int N) {
string S;
int res;
S = "A";
res = press(S);
if (res == 0) {
S = "B";
res = press(S);
if (res == 0) {
S = "X";
res = press(S);
if (res == 0) {
S = "Y";
}
}
}
for (int i = 2; i < N; i++) {
string tp;
if (S[0] == 'A') {
tp.append(S);
tp.append("B");
tp.append(S);
tp.append("XB");
tp.append(S);
tp.append("XX");
tp.append(S);
tp.append("XY");
} else if (S[0] == 'B') {
tp.append(S);
tp.append("A");
tp.append(S);
tp.append("XA");
tp.append(S);
tp.append("XX");
tp.append(S);
tp.append("XY");
} else if (S[0] == 'X') {
tp.append(S);
tp.append("A");
tp.append(S);
tp.append("BA");
tp.append(S);
tp.append("BB");
tp.append(S);
tp.append("BY");
} else {
tp.append(S);
tp.append("A");
tp.append(S);
tp.append("BA");
tp.append(S);
tp.append("BB");
tp.append(S);
tp.append("BX");
}
res = press(tp);
S.push_back(add_letter(S, res));
}
if (N == 1)
return S;
vector <char> letters = {'A', 'B', 'X', 'Y'};
for (int i = 0, done = 0; i < 4 && done < 2; i++) {
if (letters[i] == S[0])
continue;
done++;
S.push_back(letters[i]);
res = press(S);
if (res == N)
break;
S.pop_back();
}
if (S.size() < N) {
if (S[0] == 'Y')
S.push_back('X');
else
S.push_back('Y');
}
return S;
}
// int main() {
// cin >> real_S;
// cout << guess_sequence(real_S.size()) << '\n';
// }