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

int points(string me, string op) {
    if (me == op) {
        return 0;
    }
    if (me == "K") {
        return op == "P" ? -1 : 1;
    }
    if (me == "P") {
        return op == "N" ? -1 : 1;
    }
    return -points(op, me);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string who;
    cin >> who;

    int n, t;
    cin >> n >> t;

    for (int test = 0; test < t; ++test) {
        string s, read;
        cin >> s;
        bool read_mode = who == "Algosia";
        int i = 2, score = 0, emitted_in_read = 0;
        string my_move;
        while (i--) {
            if (read_mode) {
                for (int j = read.size(); j < n; ++j) {
                    if (score == 0) {
                        if (s[emitted_in_read] == '1') {
                            my_move = "P";
                        } else {
                            my_move = "K";
                        }
                        emitted_in_read++;
                    } else {
                        my_move = "K";
                    }
                    cout << my_move << flush;
                    string op_move;
                    cin >> op_move;
                    if (op_move == "K") {
                        read += "0";
                    } else {
                        read += "1";
                    }
                    score += points(my_move, op_move);
                }
            } else {
                string my_move = "K";
                for (int j = emitted_in_read; j < n; ++j) {
                    if (s[j] == '0') {
                        my_move = "K";
                    } else if (score > 0) {
                        my_move = "N";
                    } else {
                        my_move = "P";
                    }
                    cout << my_move << flush;
                    string op_move;
                    cin >> op_move;
                    if (score == 0 && (int) read.size() < n) {
                        if (op_move == "K") {
                            read += "0";
                        } else {
                            read += "1";
                        }
                    }
                    score += points(my_move, op_move);

                }
            }
            read_mode = !read_mode;
        }
        cout << "! " << read << flush;
    }

    return 0;
}