#include <stdio.h> #include <stdlib.h> #define LL long long #define BIGMOD 1000012177LL #define DNUM 31LL #define DBG(X) bool check_palindrome_L(int n) { DBG(printf("check palindromeL %d\n", n)); int i=0; LL ph = 0; LL hash = 1; for (i=0; i < n/2; i++) { LL c = getchar() - 'a'; DBG(printf("c: %lld\n", c)); ph = (ph + (c * hash)) % BIGMOD; hash = (hash * DNUM) % BIGMOD; } if (n%2 == 1) { char c = getchar(); DBG(printf("ignoring char %c as it is in the middle\n", c)); } else { DBG(printf("reached half...\n")); } i = 0; LL sh = 0; // suffix hash for (i=0; i < n/2; i++) { LL c = getchar() - 'a'; DBG(printf("c: %lld\n", c)); sh = (DNUM * sh + c) % BIGMOD; } DBG(printf("ph=%lld, sh=%lld\n", ph, sh)); return ph == sh; } struct Packed16Chars { unsigned int c0:5,c1:5,c2:5,c3:5,c4:5,c5:5,c6:5,c7:5,c8:5,c9:5,c10:5,c11:5,c12:5,c13:5,c14:5,c15:5; } __attribute__((packed)); class RollingWindow { int start_; int end_; int correspondingStartRealIdx_; int maxLength_; bool overflow_; public: RollingWindow(int maxLength) { start_ = 0; end_ = 0; correspondingStartRealIdx_ = 0; maxLength_ = maxLength; overflow_ = false; DBG(printf("Creating rolling window of size: %d\n", maxLength)); } void move1() { end_++; if (end_ == maxLength_) { overflow_ = true; end_ = 0; start_ = 1; correspondingStartRealIdx_++; } else if (overflow_) { start_++; correspondingStartRealIdx_++; if (start_ > maxLength_) { start_ = 0; } } } int translateRealIdx(int realIdx) { int translatedRealIdx = realIdx - correspondingStartRealIdx_; if (translatedRealIdx < 0) { fprintf( stderr, "ERROR translated real index <0\n"); return -1; } // must be within [0, maxLength_) translatedRealIdx += start_; translatedRealIdx %= maxLength_; //DBG(printf("translateRealIdx(%d) -> %d\n", realIdx, translatedRealIdx)); return translatedRealIdx; } }; void selftest_rw() { RollingWindow* rw = new RollingWindow(7); for (int i=0; i < 31; i++) { printf("selftest rw[%d]=%d\n", i, rw->translateRealIdx(i)); rw->move1(); } for (int i=0; i < 31; i++) { printf("selftest2 rw[%d]=%d\n", i, rw->translateRealIdx(i)); } } class StreamBuf { int chunksCount_; int chunkLength_ = 8192; int chunkLength16_; int bufLength_; int bufLength16_; Packed16Chars** buf_; int realLength_ = 0; RollingWindow* rw; public: StreamBuf(int maxLength) { DBG(printf("sizeof(Packed16Chars)=%d\n", sizeof(Packed16Chars))); bufLength_ = (maxLength / 16) + 100; bufLength16_ = maxLength; chunksCount_ = (bufLength_ / chunkLength_) + 1 ; chunkLength16_ = chunkLength_ * 16; buf_ = new Packed16Chars*[chunksCount_]; for (int i=0; i < chunksCount_; i++) { buf_[i] = (Packed16Chars*)malloc(chunkLength_ * sizeof(Packed16Chars)); DBG(printf("Allocated [%d] %d bytes at %x\n", i, chunkLength_ * sizeof(Packed16Chars), buf_[i])); if (!buf_[i]) { chunksCount_ = i; break; } } rw = new RollingWindow(chunkLength16_ * chunksCount_); DBG(printf("mem usage: %d bytes (%f MB)", bufLength_ * sizeof(Packed16Chars), 1.0 * bufLength_*sizeof(Packed16Chars) / (1024 * 1024))); } void add(unsigned char c) { // DBG(printf("stream add(%d)\n", c)); internal_place(rw->translateRealIdx(realLength_), c); rw->move1(); ++realLength_; } void fakeAdd(unsigned char c) { ++realLength_; } int getRealLength() { return realLength_; } unsigned char get(int realIdx) { int translatedRealIdx = rw->translateRealIdx(realIdx); int chunkIdx, withinChunkIdx, subIdx; to_subidx(translatedRealIdx, &chunkIdx, &withinChunkIdx, &subIdx); Packed16Chars &p = buf_[chunkIdx][withinChunkIdx]; switch (subIdx) { case 0: return p.c0; case 1: return p.c1; case 2: return p.c2; case 3: return p.c3; case 4: return p.c4; case 5: return p.c5; case 6: return p.c6; case 7: return p.c7; case 8: return p.c8; case 9: return p.c9; case 10: return p.c10; case 11: return p.c11; case 12: return p.c12; case 13: return p.c13; case 14: return p.c14; case 15: return p.c15; } return -1; } void print() { for (int jj=0; jj < realLength_; jj++) { printf("%d ", get(jj)); } printf("\n"); } private: void to_subidx(int idx, int* chunk, int* withinChunkIdx, int* subIdx) { *chunk = idx / chunkLength16_; int withinChunkIdx16 = idx % chunkLength16_; *withinChunkIdx = withinChunkIdx16 / 16; *subIdx = withinChunkIdx16 % 16; return; } void internal_place(int idx, char c) { int chunkIdx, withinChunkIdx, subIdx; to_subidx(idx, &chunkIdx, &withinChunkIdx, &subIdx); Packed16Chars &p = buf_[chunkIdx][withinChunkIdx]; switch (subIdx) { case 0: p.c0 = c; break; case 1: p.c1 = c; break; case 2: p.c2 = c; break; case 3: p.c3 = c; break; case 4: p.c4 = c; break; case 5: p.c5 = c; break; case 6: p.c6 = c; break; case 7: p.c7 = c; break; case 8: p.c8 = c; break; case 9: p.c9 = c; break; case 10: p.c10 = c; break; case 11: p.c11 = c; break; case 12: p.c12 = c; break; case 13: p.c13 = c; break; case 14: p.c14 = c; break; case 15: p.c15 = c; break; } } }; bool check_palindrom_stream(int maxLength) { // we need maxLength/4 most recent characters to keep StreamBuf stream((maxLength+3)/4); char c = getchar() - 'a'; LL ph = c % BIGMOD; stream.add(c); c = getchar(); if (c < 'a' || c > 'z') { return true; } c -= 'a'; LL sh = c % BIGMOD; stream.add(c); LL hh = 1; bool is_palindrome_now; LL i = 1; while (true) { is_palindrome_now = sh == ph; c = getchar(); if (c < 'a' || c > 'z') { break; } c -= 'a'; if (i > (maxLength + 3) / 2) { stream.fakeAdd(c); } else { stream.add(c); } if (c < 0) printf("ERROR!\n"); // If i is even (next i is odd) if (i%2 == 0) { // Add next character after first half at beginning // of 'ph' hh = (hh*DNUM) % BIGMOD; ph = (ph + hh*stream.get(i/2))%BIGMOD; // Add next character after second half at the end // of second half. sh = (sh*DNUM + c)%BIGMOD; } else { // If next i is odd (next i is even) then we // need not to change firstr, we need to remove // first character of second and append a // character to it. sh = DNUM*(sh + BIGMOD - (stream.get((i+1)/2)*hh)%BIGMOD)%BIGMOD; sh = (sh + c) % BIGMOD; } DBG(printf("c=%d ph=%lld, sh=%lld\n", c, ph, sh)); i++; DBG(stream.print()); } return is_palindrome_now; } int main() { // selftest_rw(); int n; scanf("%d\n", &n); bool is_palindrome = false; if (n == 0) { is_palindrome = check_palindrom_stream(20000000); } else { is_palindrome = check_palindrome_L(n); } printf("%s\n", is_palindrome ? "TAK" : "NIE"); 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | #include <stdio.h> #include <stdlib.h> #define LL long long #define BIGMOD 1000012177LL #define DNUM 31LL #define DBG(X) bool check_palindrome_L(int n) { DBG(printf("check palindromeL %d\n", n)); int i=0; LL ph = 0; LL hash = 1; for (i=0; i < n/2; i++) { LL c = getchar() - 'a'; DBG(printf("c: %lld\n", c)); ph = (ph + (c * hash)) % BIGMOD; hash = (hash * DNUM) % BIGMOD; } if (n%2 == 1) { char c = getchar(); DBG(printf("ignoring char %c as it is in the middle\n", c)); } else { DBG(printf("reached half...\n")); } i = 0; LL sh = 0; // suffix hash for (i=0; i < n/2; i++) { LL c = getchar() - 'a'; DBG(printf("c: %lld\n", c)); sh = (DNUM * sh + c) % BIGMOD; } DBG(printf("ph=%lld, sh=%lld\n", ph, sh)); return ph == sh; } struct Packed16Chars { unsigned int c0:5,c1:5,c2:5,c3:5,c4:5,c5:5,c6:5,c7:5,c8:5,c9:5,c10:5,c11:5,c12:5,c13:5,c14:5,c15:5; } __attribute__((packed)); class RollingWindow { int start_; int end_; int correspondingStartRealIdx_; int maxLength_; bool overflow_; public: RollingWindow(int maxLength) { start_ = 0; end_ = 0; correspondingStartRealIdx_ = 0; maxLength_ = maxLength; overflow_ = false; DBG(printf("Creating rolling window of size: %d\n", maxLength)); } void move1() { end_++; if (end_ == maxLength_) { overflow_ = true; end_ = 0; start_ = 1; correspondingStartRealIdx_++; } else if (overflow_) { start_++; correspondingStartRealIdx_++; if (start_ > maxLength_) { start_ = 0; } } } int translateRealIdx(int realIdx) { int translatedRealIdx = realIdx - correspondingStartRealIdx_; if (translatedRealIdx < 0) { fprintf( stderr, "ERROR translated real index <0\n"); return -1; } // must be within [0, maxLength_) translatedRealIdx += start_; translatedRealIdx %= maxLength_; //DBG(printf("translateRealIdx(%d) -> %d\n", realIdx, translatedRealIdx)); return translatedRealIdx; } }; void selftest_rw() { RollingWindow* rw = new RollingWindow(7); for (int i=0; i < 31; i++) { printf("selftest rw[%d]=%d\n", i, rw->translateRealIdx(i)); rw->move1(); } for (int i=0; i < 31; i++) { printf("selftest2 rw[%d]=%d\n", i, rw->translateRealIdx(i)); } } class StreamBuf { int chunksCount_; int chunkLength_ = 8192; int chunkLength16_; int bufLength_; int bufLength16_; Packed16Chars** buf_; int realLength_ = 0; RollingWindow* rw; public: StreamBuf(int maxLength) { DBG(printf("sizeof(Packed16Chars)=%d\n", sizeof(Packed16Chars))); bufLength_ = (maxLength / 16) + 100; bufLength16_ = maxLength; chunksCount_ = (bufLength_ / chunkLength_) + 1 ; chunkLength16_ = chunkLength_ * 16; buf_ = new Packed16Chars*[chunksCount_]; for (int i=0; i < chunksCount_; i++) { buf_[i] = (Packed16Chars*)malloc(chunkLength_ * sizeof(Packed16Chars)); DBG(printf("Allocated [%d] %d bytes at %x\n", i, chunkLength_ * sizeof(Packed16Chars), buf_[i])); if (!buf_[i]) { chunksCount_ = i; break; } } rw = new RollingWindow(chunkLength16_ * chunksCount_); DBG(printf("mem usage: %d bytes (%f MB)", bufLength_ * sizeof(Packed16Chars), 1.0 * bufLength_*sizeof(Packed16Chars) / (1024 * 1024))); } void add(unsigned char c) { // DBG(printf("stream add(%d)\n", c)); internal_place(rw->translateRealIdx(realLength_), c); rw->move1(); ++realLength_; } void fakeAdd(unsigned char c) { ++realLength_; } int getRealLength() { return realLength_; } unsigned char get(int realIdx) { int translatedRealIdx = rw->translateRealIdx(realIdx); int chunkIdx, withinChunkIdx, subIdx; to_subidx(translatedRealIdx, &chunkIdx, &withinChunkIdx, &subIdx); Packed16Chars &p = buf_[chunkIdx][withinChunkIdx]; switch (subIdx) { case 0: return p.c0; case 1: return p.c1; case 2: return p.c2; case 3: return p.c3; case 4: return p.c4; case 5: return p.c5; case 6: return p.c6; case 7: return p.c7; case 8: return p.c8; case 9: return p.c9; case 10: return p.c10; case 11: return p.c11; case 12: return p.c12; case 13: return p.c13; case 14: return p.c14; case 15: return p.c15; } return -1; } void print() { for (int jj=0; jj < realLength_; jj++) { printf("%d ", get(jj)); } printf("\n"); } private: void to_subidx(int idx, int* chunk, int* withinChunkIdx, int* subIdx) { *chunk = idx / chunkLength16_; int withinChunkIdx16 = idx % chunkLength16_; *withinChunkIdx = withinChunkIdx16 / 16; *subIdx = withinChunkIdx16 % 16; return; } void internal_place(int idx, char c) { int chunkIdx, withinChunkIdx, subIdx; to_subidx(idx, &chunkIdx, &withinChunkIdx, &subIdx); Packed16Chars &p = buf_[chunkIdx][withinChunkIdx]; switch (subIdx) { case 0: p.c0 = c; break; case 1: p.c1 = c; break; case 2: p.c2 = c; break; case 3: p.c3 = c; break; case 4: p.c4 = c; break; case 5: p.c5 = c; break; case 6: p.c6 = c; break; case 7: p.c7 = c; break; case 8: p.c8 = c; break; case 9: p.c9 = c; break; case 10: p.c10 = c; break; case 11: p.c11 = c; break; case 12: p.c12 = c; break; case 13: p.c13 = c; break; case 14: p.c14 = c; break; case 15: p.c15 = c; break; } } }; bool check_palindrom_stream(int maxLength) { // we need maxLength/4 most recent characters to keep StreamBuf stream((maxLength+3)/4); char c = getchar() - 'a'; LL ph = c % BIGMOD; stream.add(c); c = getchar(); if (c < 'a' || c > 'z') { return true; } c -= 'a'; LL sh = c % BIGMOD; stream.add(c); LL hh = 1; bool is_palindrome_now; LL i = 1; while (true) { is_palindrome_now = sh == ph; c = getchar(); if (c < 'a' || c > 'z') { break; } c -= 'a'; if (i > (maxLength + 3) / 2) { stream.fakeAdd(c); } else { stream.add(c); } if (c < 0) printf("ERROR!\n"); // If i is even (next i is odd) if (i%2 == 0) { // Add next character after first half at beginning // of 'ph' hh = (hh*DNUM) % BIGMOD; ph = (ph + hh*stream.get(i/2))%BIGMOD; // Add next character after second half at the end // of second half. sh = (sh*DNUM + c)%BIGMOD; } else { // If next i is odd (next i is even) then we // need not to change firstr, we need to remove // first character of second and append a // character to it. sh = DNUM*(sh + BIGMOD - (stream.get((i+1)/2)*hh)%BIGMOD)%BIGMOD; sh = (sh + c) % BIGMOD; } DBG(printf("c=%d ph=%lld, sh=%lld\n", c, ph, sh)); i++; DBG(stream.print()); } return is_palindrome_now; } int main() { // selftest_rw(); int n; scanf("%d\n", &n); bool is_palindrome = false; if (n == 0) { is_palindrome = check_palindrom_stream(20000000); } else { is_palindrome = check_palindrome_L(n); } printf("%s\n", is_palindrome ? "TAK" : "NIE"); return 0; } |