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
#include <iostream>
#include <string>

using namespace std;


int get_winner(char a, char b) {
    if (a == b) return 0;
    if ((a == 'P' && b == 'K') || (a == 'K' && b == 'N') || (a == 'N' && b == 'P')) return 1;
    return -1;
}

void solve() {
        string id_str; cin >> id_str;
        char id = id_str[0]; 
    int n, t;
    cin >> n >> t;

    while (t--) {
        
        string my_s; cin >> my_s;

        string other_s(n, ' ');
        int idxA = 0, idxB = 0;
        int d = 0; 

        while (idxA < n || idxB < n) {
            char my_move;
            
            if (id == 'A') {
                if (idxA < n) {
                    if (d == 0 || d == 1) my_move = (my_s[idxA] == '0' ? 'P' : 'K');
                    else my_move = 'P'; 
                } else {
                    my_move = (d == 1 ? 'K' : 'P'); 
                }
            } else { 
                if (idxB < n) {
                    if (d == 0 || d == -1) my_move = (my_s[idxB] == '0' ? 'P' : 'K');
                    else my_move = 'P'; 
                } else {
                    my_move = (d == -1 ? 'K' : 'P');
                }
            }

            
            cout << my_move; 
            cout.flush();
            char other_move; cin >> other_move;

            char moveA = (id == 'A' ? my_move : other_move);
            char moveB = (id == 'B' ? my_move : other_move);

            
            if (d == 0) {
                if (idxA < n) {
                    if (id == 'B') other_s[idxA] = (moveA == 'P' ? '0' : '1');
                    idxA++;
                }
                if (idxB < n) {
                    if (id == 'A') other_s[idxB] = (moveB == 'P' ? '0' : '1');
                    idxB++;
                }
            } else if (d == 1) {
                if (idxA < n) {
                    if (id == 'B') other_s[idxA] = (moveA == 'P' ? '0' : '1');
                    idxA++;
                }
            } else { 
                if (idxB < n) {
                    if (id == 'A') other_s[idxB] = (moveB == 'P' ? '0' : '1');
                    idxB++;
                }
            }
            d += get_winner(moveA, moveB);
        }
        cout << '! ' << other_s;
        cout.flush();
    }
}

int main() {
    
    solve();
    return 0;
}