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
215
216
217
218
219
220
#include <bits/stdc++.h>
#define int long long
#define a4 array<int, 4>
#define a6 array<int, 6>
using namespace std;

string name;
int n;

int rand(int mod){
  return (rand()%mod+mod)%mod;
}

void solve(){
  //dodaj bruta dla malych n

  int turn = (name == "Algosia");
  string fav_word;
  cin >> fav_word;
 vector<int> yyy;
  if(name == "Algosia")
    for(auto &s : fav_word){
      if(rand(2)) {
        if(s == '0') s = '1';
        else s = '0';
      }
    }
    else {
      for(int i=0; i<n; i++) yyy.push_back(rand(2));
    }
  //cerr <<name <<" favslowo: "<< fav_word << '\n';

  auto _52bin_to_33tern = [&](int end){
    // (e, f, g, h, i, j)
    // to [a, b, c, d]
    // where [a, b, c d]
    // corresponds to [end-5, end]
    /*
    so e+2f+4g+8h+16i+32j ==
    a + 3b + 9c + 27d
    */
    int num = 0;
    int pow = 1;
    for(int i=0; i<52; i++){
      num += pow*(fav_word[end-51+i]-'0');
      pow*=2;
    }
    vector<int> ret(33);
    for(int i=0; i<33; i++){
      ret[i] = num%3;
      num/=3;
    }
    return ret;
  };

  auto _33tern_to_52_bin = [&](deque<int> rep){
    int num = 0;
    int pow = 1;
    for(int i=0; i<33; i++){
      num += pow*(rep[i]);
      pow*=3;
    }
    vector<int> ret(52);
    for(int i=0; i<52; i++){
      ret[i] = num%2;
      num/=2;
    }
    return ret;
  };
  
  vector<int> current_to_send_3;
  if(n>=52) current_to_send_3 = _52bin_to_33tern(n-1);
  deque<int> current_received_from_enemy;
  int next_end_send_3 = n-53;
  int next_2_send=0;
  int other_2_info=0;
  int score=0;
  vector<int> enemy_answers_left;

  deque<int> back_info;
  while(enemy_answers_left.size() + back_info.size() <n || next_2_send + back_info.size()<n){
    cerr << name<<": petla " << next_2_send<< ' ' << enemy_answers_left.size() << '\n';
    if(score == -1){
      if(turn){
        array<char, 2> G={'P', 'N'};
        int offset=rand(2);
        int my = (offset + (fav_word[next_2_send]-'0'))%2;
        if (my == 1) score++;
        cout << G[my] << '\n';
        cout.flush();
        //cerr << name << ":" << G[(my_rand() + (fav_word[next_2_send]-'0'))%2] << endl;

        next_2_send++;
        char trash; cin >> trash;
        //druga osoba daje zawsze papier
      } else{
        cout << "N\n";
        cout.flush();
        char enemy; cin >> enemy;
        int enemy_offset = rand(2);
        if(enemy_offset == 0){
          enemy_answers_left.push_back(enemy=='N');
        } else {
          enemy_answers_left.push_back(enemy!='N');
        }
        if(enemy != 'N') score++;
      }
      turn^=1;
    } else if (score == 0){
      int offset = rand(3);
      array<char, 3> A = {'P', 'K', 'N'};
      if(current_to_send_3.size()==0){
        if(current_received_from_enemy.size()){
          auto tmp = _33tern_to_52_bin(current_received_from_enemy);
          for(auto t : views::reverse(tmp)) back_info.push_front(t);
        }
        current_received_from_enemy = deque<int>();
        if(next_end_send_3<=52){
          if(name == "Algosia"){
            cout << "N\n";
            cout.flush();
            char trah; cin >> trah;
            score++;
          } else{
            cout << "P\n";
            cout.flush();
            char trah; cin >> trah;
            score--;
          }
          continue;
        }
        current_to_send_3 = _52bin_to_33tern(next_end_send_3);
        next_end_send_3-=52;
      }

      int my = (current_to_send_3.back()+offset)%3;
      cout << A[my] << '\n'; //<< endl;
      //cerr <<name<<":"<< A[(current_to_send_3.back()+offset)%3] << endl;

      cout.flush();
      current_to_send_3.pop_back();
      char c;
      cin >> c;
      int num;
      if(c=='P'){
        num = 0;
      } else if (c=='K') num = 1;
      else num = 2;
      
      num-=offset;
      num+=12;
      num%=3;
      my-=offset;
      my+=12;
      my%=3;
      current_received_from_enemy.push_front(num);
      if((my + 1)%3 == num) score++;
      if(my == (num+1)%3) score--;
    } else{
      if(!turn){
        cout << "P\n";//<< endl;
        //cerr <<name<<":"<< "P" << endl;
        cout.flush();
        char enemy; cin >> enemy;
        int enemy_offset = rand(2);
        if(enemy_offset == 0){
          enemy_answers_left.push_back(enemy=='N');
        } else {
          enemy_answers_left.push_back(enemy!='N');
        }
        if(enemy != 'P') score--;
      } else {
        array<char, 2> G={'P', 'N'};
        int offset = rand(2);
        int my =(offset + (fav_word[next_2_send]-'0'))%2;
        cout << G[my] << '\n'; //<< endl;
//        cerr <<name<<":"<< G[(my_rand()%2 + (fav_word[next_2_send]-'0'))%2] << endl;
        cout.flush();
        next_2_send++;
        char trash; cin >> trash;
        //druga osoba daje zawsze nozyce
        if(my == 0) score--;
      }
      turn^=1;
    }
  }
  
  cout << "! ";

  while(back_info.size()<n){
    back_info.push_front(-1);
  }

  for(int i=0; i<enemy_answers_left.size(); i++)
    back_info[i] = enemy_answers_left[i];
  int i=0;
  for(auto &t : back_info) {
    if(name != "Algosia"){
      if(yyy[i]){
        if(t == 0) t = 1;
        else t = 0;
      }
    }
    cout << t;
    i++;
  }
  cout << '\n';
  cout.flush();
}

signed main(){
  srand(9238745);
  cin >> name;
  //cerr << "nazywam sie"<< name << '\n';
  int t; cin >> n >> t;
  //cerr <<name<<": "<< n << ' ' << t << '\n';
  while(t--){
    solve();
  }
}