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
#include<bits/stdc++.h>
#define VAR(i,n) __typeof(n) i = (n)
#define loop(i,j,s) for(int i=j;i<s;i++)
#define loopback(i,j,s) for(int i=j;i>=s;i--)
#define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++)
#define pln( x ) cout << x << "\n"
#define ps( x ) cout << x << " "
#define entr cout << "\n"
#define pcnt(i) __builtin_popcount(i)
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define SIZE(c) (c).size()
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef vector<vector<int> > VVI;
const int INFTY=20000000;
const int MAX=500100;
const int MOD=10000000;

void coutTab(int* tab,int n){
	loop(i,0,n){ cout<<tab[i]<<" "; }
	cout<<"\n";
}
template<class T> void coutVec(vector<T> tab){
	for(T t : tab){ cout<<t<<" "; }
	cout<<"\n";
}
//------------------------------------------

void sendMove(char c){ cout << c << "\n"; cout.flush(); }
char recvMove(){ char c; cin >> c; return c; }

// +1 if a wins, -1 if b wins, 0 draw. P>K>N>P
int wins(char a, char b){
	if(a == b) return 0;
	if((a=='P'&&b=='K')||(a=='K'&&b=='N')||(a=='N'&&b=='P')) return 1;
	return -1;
}

// m = number of base-3 trits needed to represent any n-bit number
int numTrits(int n){
	return (int)ceil((double)n / log2(3.0));
}

// Convert binary string s (MSB first) to m trits (LSB first, trit[0] = least significant)
// Uses repeated division by 3
VI toTrits(const string& s, int m){
	int n = s.size();
	VI num(n);
	loop(i,0,n) num[i] = s[i]-'0';
	VI trits;
	loop(_,0,m){
		int rem = 0;
		loop(j,0,n){
			int v = rem*2 + num[j];
			num[j] = v/3;
			rem = v%3;
		}
		trits.pb(rem);
	}
	return trits; // trits[0]=LSB, trits[m-1]=MSB
}

// Convert trits (LSB first) back to n-bit binary string (MSB first)
// Uses Horner's method: process from MSB trit, multiply by 3, add next trit
string fromTrits(const VI& trits, int n){
	int m = trits.size();
	VI num(n,0); // stored LSB first internally
	loopback(i,m-1,0){
		// num = num * 3 + trits[i]
		// num*3 = num<<1 + num  (binary addition with carry)
		VI orig = num;
		int carry = trits[i];
		loop(j,0,n){
			int shifted = (j>0) ? orig[j-1] : 0; // bit j of (orig<<1)
			int val = orig[j] + shifted + carry;
			num[j] = val & 1;
			carry = val >> 1;
		}
	}
	string result(n,'0');
	loop(i,0,n) result[n-1-i] = '0' + num[i]; // convert LSB-first to MSB-first string
	return result;
}

// Bidirectional simultaneous exchange using base-3 encoding.
// Both players run identical code.
// State 0: both send trit freely (0=K, 1=P, 2=N). Each decodes other's move as trit.
// If diff != 0 after round: winner plays N (gives back point), loser plays K. Both know diff.
// Expected rounds: 5/3 * n/log2(3) ~ 1.05n. Worst case: 2 * n/log2(3) ~ 1.26n.
void doExchange(const string& myStr, string& otherStr, int n){
	int m = numTrits(n);
	VI myTrits = toTrits(myStr, m);
	VI otherTrits(m, 0);
	int myDiff = 0; // my_score - their_score

	loop(i,0,m){
		// Send my trit: 0->K (draw), 1->P (tend to win), 2->N (tend to lose)
		char myMove = (myTrits[i]==0) ? 'K' : (myTrits[i]==1) ? 'P' : 'N';
		sendMove(myMove);
		char theirMove = recvMove();

		// Decode their trit from observed move
		otherTrits[i] = (theirMove=='K') ? 0 : (theirMove=='P') ? 1 : 2;

		// Update score diff
		myDiff += wins(myMove, theirMove);

		// Correction: winner plays N (lose a point back), loser plays K
		// Both compute this independently from their own myDiff
		if(myDiff != 0){
			sendMove(myDiff==1 ? 'N' : 'K');
			recvMove();
			myDiff = 0;
		}
	}

	otherStr = fromTrits(otherTrits, n);
}

int main(){
	ios_base::sync_with_stdio(0);
	string role;
	cin >> role;
	int n, t;
	cin >> n >> t;

	loop(tc,0,t){
		string myStr, otherStr(n,'?');
		cin >> myStr;
		doExchange(myStr, otherStr, n);
		cout << "! " << otherStr << "\n";
		cout.flush();
	}
}