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
155
156
157
#include <bits/stdc++.h>
// #include <cstdlib>
// #include <ctime>

using namespace std;

string algosia = "Algosia";
string username;
int n, t;

class User {
public:
    int game_score, my_buffor_idx, my_code_idx;
    string my_code, friend_code, my_buffor, friend_buffor;

    User() {
        this->my_code_idx = 0;
        this->my_buffor_idx = 0;
        this->game_score = 0;

        this->friend_code = ""; // Odkodowane 01
        this->my_buffor = ""; // Zakodowane PKN
        this->friend_buffor = ""; // Zakodowane PKN
    }

    void get_my_code() {
        cin >> this->my_code;
        while (this->my_code.size() % 3) {
            this->my_code += "0";
        }
    }

    bool should_encrypt_msg() {
        return this->game_score == 0;
    }

    bool should_me_win_game() {
        return (
            (this->game_score == -1 && username == algosia) ||
            (this->game_score == 1 && username != algosia)
        );
    }

    void win_game() {
        cout << "P\n";
    }

    void lose_game() {
        cout << "K\n";
    }

    string decode_friend_buffor() {
        if (this->friend_buffor == "PP") return "000";
        if (this->friend_buffor == "PK") return "001";
        if (this->friend_buffor == "PN") return "010";
        if (this->friend_buffor == "KP") return "011";
        if (this->friend_buffor == "KK") return "100";
        if (this->friend_buffor == "KN") return "101";
        if (this->friend_buffor == "NP") return "110";
        if (this->friend_buffor == "NK") return "111";
    }

    string create_my_buffor() {
        string substring = this->my_code.substr(this->my_code_idx, 3);
        this->my_code_idx += 3;
        this->my_buffor_idx = 0;
        if (substring == "000") return "PP";
        if (substring == "001") return "PK";
        if (substring == "010") return "PN";
        if (substring == "011") return "KP";
        if (substring == "100") return "KK";
        if (substring == "101") return "KN";
        if (substring == "110") return "NP";
        if (substring == "111") return "NK";
    }

    char send_msg() {
        char output = this->my_buffor[this->my_buffor_idx];
        cout << output << '\n';
        cout.flush();

        this->my_buffor_idx++;
        return output;
    }

    char receive_msg() {
        char msg; cin >> msg;
        this->friend_buffor += msg;
        if (this->friend_buffor.size() == 2) {
            this->friend_code += this->decode_friend_buffor();
            this->friend_buffor = "";
        }
        return msg;
    }

    void update_game_score(char my_msg, char friend_msg) {
        if (my_msg == friend_msg) return;
        int output = 0;
        if (
            (my_msg == 'P' && friend_msg == 'K') ||
            (my_msg == 'K' && friend_msg == 'N') ||
            (my_msg == 'N' && friend_msg == 'P')
        ) ++output;
        else --output;

        if (username != algosia) output *= -1;
        this->game_score += output;
    }

    int finish() {
        this->friend_code = this->friend_code.substr(0, n);
        cout << "! " << this->friend_code << '\n';
        cout.flush();
        return 1;
    }

    int make_move() {
        if (this->my_buffor_idx >= this->my_buffor.size()) {
            if (this->my_code_idx >= n) {
                return this->finish();
            }
            this->my_buffor = this->create_my_buffor();
        }

        if (should_encrypt_msg()) {
            char my_msg = send_msg();
            char friend_msg = receive_msg();
            update_game_score(my_msg, friend_msg);
        }
        else {
            should_me_win_game() ? win_game() : lose_game();
            cout.flush();
            this->game_score = 0;
            char tmp; cin >> tmp;
        }

        return 0;
    }
};


int main() {
    srand(time(NULL));

    cin >> username;
    cin >> n >> t;
    for (int i = 0; i < t; ++i) {
        User user;
        user.get_my_code();

        int status = 0;
        do {
            status = user.make_move();
        } while (status == 0);
    }
    return 0;
}