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

#define rep(a,b,c) for(auto a = (b); a != (c); a++)
#define repD(a,b,c) for(auto a = (b); a != (c); a--)
#define repIn(a, b) for(auto& a : (b))
#define repIn2(a, b, c) for(auto& [a, b] : (c))

constexpr bool dbg = 0;
#define DEBUG if constexpr(dbg)
#define DC DEBUG std::cerr
#define eol std::endl

#define int long long
#define ld long double
#define pb push_back

using namespace std;
#define rg ranges

//                   <    <    <    <
array<char, 3> mvs = {{'K', 'P', 'N'}};
array<int, 3> perm, perm2;
constexpr int MAX = 3160;
//constexpr int MAX = 5;

// lekko zmodyfikowany kod z https://stackoverflow.com/questions/12015752/conversion-of-binary-bitstream-to-and-from-ternary-bitstream/12016140#12016140
void to_trits(vector<int> bits, vector<int>& trits, int n_trits=MAX)
{
    int n_bits = (int) bits.size();
    trits.resize(n_trits, 0);
    int i_trit, i_bit;
    for (i_trit = 0; i_trit < n_trits; i_trit++)
        trits[i_trit] = 0;
    // Scan bits left to right.
    for (i_bit = 0; i_bit < n_bits; i_bit++) {
        int bit = bits[i_bit];
        // Update the trit representation
        trits[0] = trits[0] * 2 + bit;
        for (i_trit = 1; i_trit < n_trits; i_trit++) {
            trits[i_trit] *= 2;
            if (trits[i_trit - 1] > 2) {
                trits[i_trit - 1] -= 3;
                trits[i_trit]++;
            }
        }
    }
}

void from_trits(vector<int> trits, vector<int>& bits) {
    while(trits.size()) {
        int bit = 0;
        repIn(i, trits) bit ^= i & 1;
        bits.pb(bit);
        vector<int> trits2;
        int carry = 0;
        rg::reverse(trits);
        repIn(i, trits) {
            carry = carry * 3 + i;
            if(carry / 2 || trits2.size()) trits2.pb(carry / 2);
            carry %= 2;
        }
        rg::reverse(trits2);
        trits = trits2;
    }
}

int32_t main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    string xd;
    cin >> xd;

    /*
    vector<int> a;
    repIn(i, xd) a.pb(i - '0');
    vector<int> b, c;
    to_trits(a, b);
    repIn(i, b) cout << i;
    cout << eol;
    from_trits(b, c);
    repIn(i, c) cout << i;
    cout << eol;
    */

    int n, t;
    cin >> n >> t;
    while(t--) {
        string s;
        cin >> s;
        DEBUG {
            stringstream ss;
            ss << "DC0 " << xd << ' ';
            DEBUG repIn(i, s) ss << i;
            ss << '\n';
            DC << ss.str() << eol;
        }
        // cout << "! " << s << eol;
        // continue;
        vector<int> bits(n), toSend;
        rep(i, 0, n) bits[i] = s[i] - '0';
        DEBUG {
            stringstream ss;
            ss << "DC1 " << xd << ' ';
            DEBUG repIn(i, bits) ss << i;
            ss << '\n';
            DC << ss.str() << eol;
        }
        toSend.clear();
        to_trits(bits, toSend);
        DEBUG {
            stringstream ss;
            ss << "DC2 " << xd << ' ';
            DEBUG repIn(i, toSend) ss << i;
            ss << '\n';
            DC << ss.str() << eol;
        }
        int sid = 2137 + t;
        mt19937 rnd(sid);
        rep(i, 0, 3) perm[i] = i;
        int sid2 = 69 + 15 * t;
        mt19937 rnd2(sid2);
        rep(i, 0, 3) perm2[i] = i;
        vector<int> trits;
        repIn(i, toSend) {
            rg::shuffle(perm, (xd[0] == 'A' ? rnd : rnd2));
            rg::shuffle(perm2, (xd[0] == 'B' ? rnd : rnd2));
            auto my = mvs[perm[i]];
            cout << my << eol;
            char his;
            cin >> his;
            rep(j, 0, 3) if(mvs[perm2[j]] == his) trits.pb(j);
            if(his == my) continue;
            cout << his << eol;
            cin >> his;
        }
        DEBUG {
            stringstream ss;
            ss << "DC3 " << xd << ' ';
            DEBUG repIn(i, trits) ss << i;
            ss << '\n';
            DC << ss.str() << eol;
        }
        bits.clear();
        from_trits(trits, bits);
        while((int)bits.size() > n) bits.pop_back();
        while((int)bits.size() < n) bits.push_back(0);
        rg::reverse(bits);
        DEBUG {
            stringstream ss;
            ss << "DC4 " << xd << ' ';
            DEBUG repIn(i, bits) ss << i;
            ss << '\n';
            DC << ss.str() << eol;
        }
        cout << "! ";
        repIn(i, bits) cout << i;
        cout << eol;
    }
}