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
// PA2026, @mjm, r2a-pkn

#include <cstdio>
#include <cmath>
#include <vector>
#include <deque>
using namespace std;

using lol = long long;
const int p = 1000000000 + 7;
const int N = 5000 + 99;
int player;
int n;
char myCode[N];
char othCode[N];

const char* pnk = "PNK";

void msgWrite(int value) {
	printf("%c\n", pnk[value]);
	fflush(stdout);
}
int msgRead() {
	char buf[16];
	scanf("%s", buf);
	if ('P' == buf[0]) return 0;
	if ('N' == buf[0]) return 1;
	return 2;
}

lol seed;
inline void nextSeed() { seed = (seed * 134775813 + 1) % p; }
const lol mask = 1 << 13;
const lol zero = 0;
inline int nextRandomBit() { return (seed & mask) != 0 ? 1 : 0; }

int encodeVal3(char* src, int& pos) {
	int randomBit = nextRandomBit();
	if (src[pos] == '0' + randomBit) {
		++pos;
		return 2;
	}
	if (src[pos + 1] == '0') {
		pos += 2;
		return 0;
	}
	pos += 2;
	return 1;
}
void decodeVal3(char* dst, int& pos, int val) {
	int randomBit = nextRandomBit();
	if (2 == val) {
		dst[pos] = '0' + randomBit;
		++pos;
		return;
	}
	dst[pos] = '0' + (1 - randomBit);
	++pos;
	dst[pos] = '0' + val;
	++pos;
}
int encodeVal2(char* src, int& pos) {
	int randomBit = nextRandomBit();
	if (src[pos] == '0' + randomBit) {
		++pos;
		return 0;
	}
	++pos;
	return 1;
}
void decodeVal2(char* dst, int& pos, int val) {
	int randomBit = nextRandomBit();
	if (0 == val) {
		dst[pos] = '0' + randomBit;
		++pos;
		return;
	}
	dst[pos] = '0' + (1 - randomBit);
	++pos;
}

void updateScores(int& myScore, int& othScore, int myVal, int othVal) {
	if (myVal == othVal)
		return;
	if (myVal == ((othVal + 1) % 3))
		++myScore;
	else
		++othScore;
}

void singleCase() {
	scanf("%s", myCode);
	int myPos = 0;
	int othPos = 0;
	int myScore = 0;
	int othScore = 0;

	while (myPos < n || othPos < n) {
		nextSeed();

		if (myScore < othScore) {
			// I need to win (or draw)!

			// who is sending information?
			bool iAmSending = false;
			if (myPos < othPos) iAmSending = true;
			if (myPos == othPos && player == 0) iAmSending = true;

			if (iAmSending) {
				// I send "01", oth send "0"
				int myVal = encodeVal2(myCode, myPos);
				msgWrite(myVal);
				int othVal = msgRead();
				// assert(othVal == 0)
				updateScores(myScore, othScore, myVal, othVal);
			} else {
				// I send "1", oth send "01"
				int myVal = 1;
				msgWrite(myVal);
				int othVal = msgRead();
				decodeVal2(othCode, othPos, othVal);
				updateScores(myScore, othScore, myVal, othVal);
			}
			continue;
		}

		if (myScore > othScore) {
			// I need to loss (or draw)!

			// who is sending information?
			bool iAmSending = false;
			if (myPos < othPos) iAmSending = true;
			if (myPos == othPos && player == 0) iAmSending = true;

			if (iAmSending) {
				// I send "01", oth send "1"
				int myVal = encodeVal2(myCode, myPos);
				msgWrite(myVal);
				int othVal = msgRead();
				// assert(othVal == 1)
				updateScores(myScore, othScore, myVal, othVal);
			}
			else {
				// I send "0", oth send "01"
				int myVal = 0;
				msgWrite(myVal);
				int othVal = msgRead();
				decodeVal2(othCode, othPos, othVal);
				updateScores(myScore, othScore, myVal, othVal);
			}
			continue;
		}

		// round 3x3
		int myVal = encodeVal3(myCode, myPos);
		msgWrite(myVal);
		int othVal = msgRead();
		decodeVal3(othCode, othPos, othVal);
		updateScores(myScore, othScore, myVal, othVal);
	}
}

int main() {
	seed = 20260325094847LL;

	char name[16];
	int TC;
	scanf("%s%d%d", name, &n, &TC);
	player = name[0] == 'A' ? 0 : 1;

	for (int tc = 0; tc < TC; ++tc) {
		singleCase();
		othCode[n] = 0;
		printf("! %s\n", othCode);
		fflush(stdout);
	}

	return 0;
}