#include <cstdio> #include <cmath> #include <iostream> #include <algorithm> #include <queue> #include <list> #include <vector> #include <map> #include <set> #include <string> #include <cstring> #include <cassert> #include <limits.h> using namespace std; #define ST first #define ND second #define MP make_pair #define PB push_back #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(i,a,b) for(VAR(i,a);i<=(b);++i) #define FORD(i,a,b) for(VAR(i,a);i>=(b);--i) #define FORE(a,b) for(VAR(a,(b).begin());a!=(b).end();++a) #define VAR(a,b) __typeof(b) a=(b) #define SIZE(a) ((int)((a).size())) #define ALL(x) (x).begin(),(x).end() #define CLR(x,a) memset(x,a,sizeof(x)) #define ZERO(x) memset(x,0,sizeof(x)) #define S(t,i) scanf("%" ## t, &i) #define SI(i) scanf("%d", &i) const int MAXN=600000; const unsigned int PRIME=2149840313; const int BASE=26; unsigned int p[MAXN]; const int MAXB = 16384; char b[MAXB]; int n; bool brute(char * b, int n) { REP(bi, n/2) { if (b[bi] != b[n-bi-1]) { printf("NIE\n"); return false; } } return true; } int readChunk(char *b, int chunkSize) { int readSize = (int) fread(b, 1, chunkSize, stdin); while (readSize>0 && (b[readSize-1]<'a' || b[readSize-1]>'z')) { readSize--; } return readSize; } unsigned long long int pow(int base, int exp) { unsigned long long int result = 1; REP(i, exp) { result = result*base % PRIME; } return result; } int main() { ios_base::sync_with_stdio(0); scanf("%d ", &n); if (n) { if (n <= 2*MAXN+1) { char * pc = (char *) p; int readSize = 0; while (readSize < n/2) { readSize += fread(pc + readSize, 1, min(n/2-readSize, MAXB), stdin); } if (n & 1) { fread(b, 1, 1, stdin); } int pi = n/2; while (pi) { unsigned long bl = fread(b, 1, min(pi, MAXB), stdin); REP(bi, bl) { if (pc[--pi] != b[bi]) { printf("NIE\n"); return 0; } } } } else { int half = n/2; int stride = (half+MAXN-1)/MAXN; int pi = 0; unsigned long long int h; while ((pi + 1) * stride < half) { fread(b, 1, stride, stdin); h = 0; REP(bi, stride) { h = (h*BASE + (b[bi] - 'a')) % PRIME; } p[pi] = (unsigned int) h; pi++; } int center = 2*(half - pi*stride) + (n&1); fread(b, 1, center, stdin); if (!brute(b, center)) { return 0; } while (pi > 0) { fread(b, 1, stride, stdin); h = 0; long long int basePower = 1; REP(bi, stride) { h = (h + (b[bi] - 'a') * basePower) % PRIME; basePower = (basePower * BASE) % PRIME; } if (h != p[--pi]) { printf("NIE\n"); return 0; } } } } else { unsigned int *p1 = p; unsigned int *p2 = p + MAXN/2; int stride1 = 67, stride2 = 68; int chunkSize = MAXB; int readSize = readChunk(b, chunkSize); if (feof(stdin)) { if (!brute(b, readSize)) { return 0; } printf("TAK\n"); return 0; } unsigned long long int h1 = 0, h2 = 0; unsigned long long int basePower = 1; int pi1 = 0, pi2 = 0; while (readSize) { REP(bi, readSize) { char c = b[bi] - 'a'; h1 = (h1*BASE + c) % PRIME; h2 = (h2 + c * basePower) % PRIME; basePower = (basePower * BASE) % PRIME; ++n; if (n % stride1 == 0) { p1[pi1++] = (unsigned int) h1; h1 = 0; } if (n % stride2 == 0) { p2[pi2++] = (unsigned int) h2; } } readSize = readChunk(b, chunkSize); } int rest1 = n % stride1; if (rest1) { p1[pi1++] = (unsigned int) h1; } else { rest1 = stride1; } int rest2 = n % stride2; if (rest2) { p2[pi2++] = (unsigned int) h2; } unsigned long long int stridePow = pow(BASE, stride1); unsigned long long int x = pow(BASE, rest1); FORD(i, pi1-1, 1) { h1 = (p1[i] + x * p1[i-1]) % PRIME; p1[i-1] = (int) h1; x = x * stridePow % PRIME; } if (h1 != h2) { printf("NIE\n"); return 0; } int i1 = rest1; int i2 = stride2; pi2 = 0; pi1--; while (i1<n && i2<n) { if (i1 == i2) { if (p1[pi1] != p2[pi2]) { printf("NIE\n"); return 0; } // printf("%d %d\n", pi1, pi2); i1 += stride1; pi1--; i2 += stride2; pi2++; } else if (i1 < i2) { i1 += stride1; pi1--; } else { i2 += stride2; pi2++; } } } printf("TAK\n"); return 0; }
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 | #include <cstdio> #include <cmath> #include <iostream> #include <algorithm> #include <queue> #include <list> #include <vector> #include <map> #include <set> #include <string> #include <cstring> #include <cassert> #include <limits.h> using namespace std; #define ST first #define ND second #define MP make_pair #define PB push_back #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(i,a,b) for(VAR(i,a);i<=(b);++i) #define FORD(i,a,b) for(VAR(i,a);i>=(b);--i) #define FORE(a,b) for(VAR(a,(b).begin());a!=(b).end();++a) #define VAR(a,b) __typeof(b) a=(b) #define SIZE(a) ((int)((a).size())) #define ALL(x) (x).begin(),(x).end() #define CLR(x,a) memset(x,a,sizeof(x)) #define ZERO(x) memset(x,0,sizeof(x)) #define S(t,i) scanf("%" ## t, &i) #define SI(i) scanf("%d", &i) const int MAXN=600000; const unsigned int PRIME=2149840313; const int BASE=26; unsigned int p[MAXN]; const int MAXB = 16384; char b[MAXB]; int n; bool brute(char * b, int n) { REP(bi, n/2) { if (b[bi] != b[n-bi-1]) { printf("NIE\n"); return false; } } return true; } int readChunk(char *b, int chunkSize) { int readSize = (int) fread(b, 1, chunkSize, stdin); while (readSize>0 && (b[readSize-1]<'a' || b[readSize-1]>'z')) { readSize--; } return readSize; } unsigned long long int pow(int base, int exp) { unsigned long long int result = 1; REP(i, exp) { result = result*base % PRIME; } return result; } int main() { ios_base::sync_with_stdio(0); scanf("%d ", &n); if (n) { if (n <= 2*MAXN+1) { char * pc = (char *) p; int readSize = 0; while (readSize < n/2) { readSize += fread(pc + readSize, 1, min(n/2-readSize, MAXB), stdin); } if (n & 1) { fread(b, 1, 1, stdin); } int pi = n/2; while (pi) { unsigned long bl = fread(b, 1, min(pi, MAXB), stdin); REP(bi, bl) { if (pc[--pi] != b[bi]) { printf("NIE\n"); return 0; } } } } else { int half = n/2; int stride = (half+MAXN-1)/MAXN; int pi = 0; unsigned long long int h; while ((pi + 1) * stride < half) { fread(b, 1, stride, stdin); h = 0; REP(bi, stride) { h = (h*BASE + (b[bi] - 'a')) % PRIME; } p[pi] = (unsigned int) h; pi++; } int center = 2*(half - pi*stride) + (n&1); fread(b, 1, center, stdin); if (!brute(b, center)) { return 0; } while (pi > 0) { fread(b, 1, stride, stdin); h = 0; long long int basePower = 1; REP(bi, stride) { h = (h + (b[bi] - 'a') * basePower) % PRIME; basePower = (basePower * BASE) % PRIME; } if (h != p[--pi]) { printf("NIE\n"); return 0; } } } } else { unsigned int *p1 = p; unsigned int *p2 = p + MAXN/2; int stride1 = 67, stride2 = 68; int chunkSize = MAXB; int readSize = readChunk(b, chunkSize); if (feof(stdin)) { if (!brute(b, readSize)) { return 0; } printf("TAK\n"); return 0; } unsigned long long int h1 = 0, h2 = 0; unsigned long long int basePower = 1; int pi1 = 0, pi2 = 0; while (readSize) { REP(bi, readSize) { char c = b[bi] - 'a'; h1 = (h1*BASE + c) % PRIME; h2 = (h2 + c * basePower) % PRIME; basePower = (basePower * BASE) % PRIME; ++n; if (n % stride1 == 0) { p1[pi1++] = (unsigned int) h1; h1 = 0; } if (n % stride2 == 0) { p2[pi2++] = (unsigned int) h2; } } readSize = readChunk(b, chunkSize); } int rest1 = n % stride1; if (rest1) { p1[pi1++] = (unsigned int) h1; } else { rest1 = stride1; } int rest2 = n % stride2; if (rest2) { p2[pi2++] = (unsigned int) h2; } unsigned long long int stridePow = pow(BASE, stride1); unsigned long long int x = pow(BASE, rest1); FORD(i, pi1-1, 1) { h1 = (p1[i] + x * p1[i-1]) % PRIME; p1[i-1] = (int) h1; x = x * stridePow % PRIME; } if (h1 != h2) { printf("NIE\n"); return 0; } int i1 = rest1; int i2 = stride2; pi2 = 0; pi1--; while (i1<n && i2<n) { if (i1 == i2) { if (p1[pi1] != p2[pi2]) { printf("NIE\n"); return 0; } // printf("%d %d\n", pi1, pi2); i1 += stride1; pi1--; i2 += stride2; pi2++; } else if (i1 < i2) { i1 += stride1; pi1--; } else { i2 += stride2; pi2++; } } } printf("TAK\n"); return 0; } |