#include <iostream>
using namespace std;
#define MAX_N (long long) 2e7
#define CURR_N (long long) 2e6
int n;
char x, curr[CURR_N + 3];
long long PRIME = 31, scoresLeft[MAX_N / CURR_N + 3], scoresRight[MAX_N / CURR_N + 3], amount;
long long calcHash(int pos) {
long long res = 0;
for (int i = 0; i < pos; i++) {
res = res * PRIME + curr[i];
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
scanf("%d\n", &n);
for (int i = 1, posLeft = 0, scoresPos = 0, posRight = 0; i <= n; i++) {
scanf("%c", &x);
x -= 'a';
if (i <= n / 2) {
if (posLeft < CURR_N) {
curr[posLeft++] = x;
}
if (posLeft == CURR_N) {
scoresLeft[scoresPos++] = calcHash(posLeft);
posLeft = 0;
amount++;
}
} else if (i > (n + 1) / 2) {
if (posRight >= 0) {
curr[posRight--] = x;
posLeft++;
}
if (posRight == -1) {
scoresRight[--scoresPos] = calcHash(posLeft);
posRight = CURR_N - 1;
posLeft = 0;
}
}
if ((n % 2 == 0 && 2 * i == n) || (n % 2 == 1 && 2 * i == n + 1)) {
if (posLeft) {
scoresLeft[scoresPos++] = calcHash(posLeft);
}
posRight = (posLeft ? posLeft : CURR_N) - 1;
posLeft = 0;
amount++;
}
if (i == n) {
if (posLeft) {
scoresRight[--scoresPos] = calcHash(posLeft);
}
}
}
for (int i = 0; i < amount; i++) {
if (scoresLeft[i] != scoresRight[i]) {
cout << "NIE";
return 0;
}
}
cout << "TAK";
}
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 | #include <iostream> using namespace std; #define MAX_N (long long) 2e7 #define CURR_N (long long) 2e6 int n; char x, curr[CURR_N + 3]; long long PRIME = 31, scoresLeft[MAX_N / CURR_N + 3], scoresRight[MAX_N / CURR_N + 3], amount; long long calcHash(int pos) { long long res = 0; for (int i = 0; i < pos; i++) { res = res * PRIME + curr[i]; } return res; } int main() { ios_base::sync_with_stdio(0); scanf("%d\n", &n); for (int i = 1, posLeft = 0, scoresPos = 0, posRight = 0; i <= n; i++) { scanf("%c", &x); x -= 'a'; if (i <= n / 2) { if (posLeft < CURR_N) { curr[posLeft++] = x; } if (posLeft == CURR_N) { scoresLeft[scoresPos++] = calcHash(posLeft); posLeft = 0; amount++; } } else if (i > (n + 1) / 2) { if (posRight >= 0) { curr[posRight--] = x; posLeft++; } if (posRight == -1) { scoresRight[--scoresPos] = calcHash(posLeft); posRight = CURR_N - 1; posLeft = 0; } } if ((n % 2 == 0 && 2 * i == n) || (n % 2 == 1 && 2 * i == n + 1)) { if (posLeft) { scoresLeft[scoresPos++] = calcHash(posLeft); } posRight = (posLeft ? posLeft : CURR_N) - 1; posLeft = 0; amount++; } if (i == n) { if (posLeft) { scoresRight[--scoresPos] = calcHash(posLeft); } } } for (int i = 0; i < amount; i++) { if (scoresLeft[i] != scoresRight[i]) { cout << "NIE"; return 0; } } cout << "TAK"; } |
English