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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// clang-format off
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(auto i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
auto operator<<(ostream&o,auto p)->decltype(p.first,o){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(ostream&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<"}";}
ostream &operator<<(ostream &o, const vector<short> &ternary);
#ifdef DEBUG
#define cerr log_file
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<endl
#else
#define debug(...) {}
#endif
// clang-format on

const int MAX_BITS = 5000;
mt19937 rng(1241928734);
bitset<MAX_BITS> XOR_KEY;

char name;
int msg_len, ntests;
ofstream log_file;

void input_msg(bitset<MAX_BITS> &msg, int len);
void output_msg(const bitset<MAX_BITS> &msg, int len);

struct TestCase
{
    constexpr static const char PKN[3] = {'P', 'K', 'N'};

    int nbits_sent     = 0;  // number of bits that they have of my msg
    int nbits_received = 0;  // number of bits that I have of their msg
    int points_balance = 0;
    bitset<MAX_BITS> my_msg;
    bitset<MAX_BITS> their_msg;

    TestCase(char name)
    {
        input_msg(my_msg, msg_len);
        debug(my_msg);
    }

    bool I_know_their_msg() { return nbits_received >= msg_len; }
    bool they_know_our_msg() { return nbits_sent >= msg_len; }
    bool we_know_our_messages()
    {
        return I_know_their_msg() && they_know_our_msg();
    }

    char send_ternary()
    {
        if (nbits_sent >= msg_len) return PKN[0];
        int val = my_msg[nbits_sent++];
        if (nbits_sent >= msg_len or val == 1) return PKN[val];
        if (my_msg[nbits_sent]) val = 2;
        nbits_sent += 1;
        return PKN[val];
    }
    void receive_ternary(char they_sent)
    {
        if (nbits_received >= msg_len) return;
        int val = (they_sent == PKN[0] ? 0 : (they_sent == PKN[1] ? 1 : 2));
        their_msg[nbits_received++] = val & 1;
        if (nbits_received >= msg_len or val == 1) return;
        their_msg[nbits_received++] = val >> 1;
    }
    char send_binary()
    {
        if (nbits_sent >= msg_len) return PKN[0];
        return PKN[my_msg[nbits_sent++]];
    }
    void receive_binary(char they_sent)
    {
        if (nbits_received >= msg_len) return;
        int val                     = (they_sent == PKN[1] ? 1 : 0);
        their_msg[nbits_received++] = val;
    }

    int competition_result(char I_sent, char they_sent);
    void perform_competition(char I_sent, char they_sent)
    {
        auto result = competition_result(I_sent, they_sent);
        points_balance += result;
        debug(I_sent, they_sent, result, points_balance);
        assert(points_balance >= -1 && points_balance <= 1);
    }
};


void
test_case()
{
    TestCase data(name);
    int nqueries = 0;
    while (!data.we_know_our_messages()) {
        char I_send, they_sent;
        bool I_choose_now = data.nbits_sent < data.nbits_received;
        if (data.nbits_received == data.nbits_sent)
            I_choose_now = (name == 'A');  // break ties depending on name
        debug(nqueries, data.nbits_sent, data.nbits_received, I_choose_now);

        // we both can send 1.5bits
        if (data.points_balance == 0) {
            I_send = data.send_ternary();
            cout << I_send << endl;
            cin >> they_sent;
            data.receive_ternary(they_sent);
        } else if (I_choose_now) {
            debug(I_choose_now);
            I_send = data.send_binary();
            cout << I_send << endl;
            cin >> they_sent;
        } else {
            debug(I_choose_now);
            I_send = (data.points_balance > 0 ? 'K' : 'P');
            cout << I_send << endl;
            cin >> they_sent;
            data.receive_binary(they_sent);
        }

        data.perform_competition(I_send, they_sent);
        nqueries += 1;
    }

    debug(data.their_msg);
    //if (name == 'A') cerr << nqueries << " ";
    cout << "! ";
    output_msg(data.their_msg, msg_len);
    cout << endl;
}

int
main()
{
    // Generating key for encryption
    REP (i, MAX_BITS) XOR_KEY[i] = rng() & 1;

    string full_name;
    cin.tie(0)->sync_with_stdio(0);
    cin >> full_name >> msg_len >> ntests;
    name = full_name[0];

#ifdef DEBUG
    log_file.open(full_name + ".err");
#endif

    while (ntests--) test_case();

#ifdef DEBUG
    log_file.close();
#endif
    return 0;
}

int
TestCase::competition_result(char I_sent, char they_sent)
{
    switch (I_sent) {
        case 'P': {
            switch (they_sent) {
                case 'P':
                    return 0;
                case 'K':
                    return 1;
                case 'N':
                    return -1;
            }
        }
        case 'K': {
            switch (they_sent) {
                case 'P':
                    return -1;
                case 'K':
                    return 0;
                case 'N':
                    return 1;
            }
        }
        case 'N': {
            switch (they_sent) {
                case 'P':
                    return 1;
                case 'K':
                    return -1;
                case 'N':
                    return 0;
            }
        }
    }
    return 0;
}

// ================================ Utility Log ================================

void
input_msg(bitset<MAX_BITS> &msg, int len)
{
    REP (i, len) {
        char ch;
        cin >> ch;
        msg[len - i - 1] = (ch == '1');
    }
    msg ^= XOR_KEY;
}

void
output_msg(const bitset<MAX_BITS> &msg, int len)
{
    auto res = msg ^ XOR_KEY;
    REP (i, len) cout << (res[len - i - 1] ? '1' : '0');
}