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
#include<bits/stdc++.h>
using namespace std;

string name, s, ans, randAB[2] = {"10211002110", "02001012111"};
char zna[3] = {'P', 'N', 'K'}, mess;
map<char, int> revzna;
int gameres[3][3] = {{0, -1, 1}, {1, 0, -1}, {-1, 1, 0}}, wyn = 0;

void equate()
{
	if(wyn == 1) cout<<'P'<<endl;
	if(wyn == -1) cout<<'N'<<endl;
	wyn = 0;
	cin>>mess;
	return;
}

int main()
{
    int n, t, k, a, b, d, x, y, r = 0, z;
    cin>>name>>n>>t;
    z = name[0] - 'A';
    revzna['P'] = 0;
    revzna['N'] = 1;
    revzna['K'] = 2;
    while(t--) {
		cin>>s;
		ans.clear();
		wyn = k = r = 0;
		while(n - k >= 3) {
			a = 4 * (s[k] - '0') + 2 * (s[k+1] - '0') + (s[k+2] - '0');
			b = a / 3;
			d = a % 3;
			b += (randAB[z][r] - '0');
			if(b > 2) b -= 3;
			cout<<zna[b]<<endl;
			cin>>mess;
			y = revzna[mess];
			y -= (randAB[1-z][r] - '0');
			if(y < 0) y += 3;
			r++;
			if(r >= 11) r -= 11;
			x = y;
			wyn += gameres[b][revzna[mess]];
			if(wyn != 0) equate();
			
			d += (randAB[z][r] - '0');
			if(d > 2) d -= 3;
			cout<<zna[d]<<endl;
			cin>>mess;
			y = revzna[mess];
			y -= (randAB[1-z][r] - '0');
			if(y < 0) y += 3;
			r++;
			if(r >= 11) r -= 11;
			x = 3 * x + y;
			wyn += gameres[d][revzna[mess]];
			if(wyn != 0) equate();
			
			if(x / 4 == 1) ans.push_back('1');
			else ans.push_back('0');
			if((x / 2) % 2 == 1) ans.push_back('1');
			else ans.push_back('0');
			if(x % 2 == 1) ans.push_back('1');
			else ans.push_back('0');
			k += 3;
		}
		while(k < n) {
			cout<<zna[s[k]-'0']<<endl;
			cin>>mess;
			if(mess == 'N') ans.push_back('1');
			else ans.push_back('0');
			wyn += gameres[s[k]-'0'][revzna[mess]];
			if(k != n - 1 && wyn != 0) equate();
			k++;
		}
		cout<<"! "<<ans<<endl;
	}
    return 0;
}