#include <vector> #include <cstdio> #define ll long long const int TS = 1032321; using namespace std; struct HashC { ll x; ll mod; ll f; ll r; ll cur; HashC(ll x, ll mod): x(x), mod(mod), f(0), r(0), cur(1) {} void update(char c) { f = (f*x)%mod; f = (f+c)%mod; ll d = (c * cur) % mod; cur = (cur*x)%mod; r = (r+d)%mod; } bool ok() { return f == r; } }; struct TwoBuffers { char* be; char* en; int b; int e; int size; int read; TwoBuffers(int size): be(new char[size]), en(new char[size]), size(size), b(0), e(0), read(0) {} void update(char c) { if(b < size) { be[b] = c; b++; } else { en[e] = c; e = (e+1)%size; } read++; } char fromBeginning(int i) { return be[i]; } char fromEnd(int i) { if(read >= 2*size) { return en[(e + 3*size - 1 - i) % size]; } else if(read >= size) { int k = e - i - 1; if(k < 0) { return be[k + size]; } else { return en[k]; } } else { return be[b - i - 1]; } } }; bool solve1() { char* buf = new char[TS + 117]; char c; int s = 0; for(;;) { c = getchar(); if(c < 'a' || c > 'z') { break; } buf[s] = c; s++; } int s2 = s/2; for(int i=0;i<s2;i++) { if(buf[i] != buf[s-1-i]) { return false; } } return true; } bool solve2() { TwoBuffers tb(131072); vector<HashC> hashes; hashes.push_back(HashC(951751951LL,1000000007LL)); hashes.push_back(HashC(0x1111BEEFLL,1013904223LL)); int hs = hashes.size(); char c; for(;;) { c = getchar(); if(c < 'a' || c > 'z') { break; } tb.update(c); for(int i=0;i<hs;i++) { hashes[i].update(c); } } bool ok = true; for(int i=0;i<hs;i++) { if(!hashes[i].ok()) { ok = false; break; } } if(ok) { int z = min(tb.size - 5, tb.read / 2); for(int i=0;i<z;i++) { if(tb.fromBeginning(i) != tb.fromEnd(i)) { ok = false; break; } } } return ok; } int main(int argc, char** argv) { int n; scanf("%d\n",&n); bool ok = false; if((n > 0) && (n <= TS)) { ok = solve1(); } else { ok = solve2(); } printf(ok?"TAK\n":"NIE\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 | #include <vector> #include <cstdio> #define ll long long const int TS = 1032321; using namespace std; struct HashC { ll x; ll mod; ll f; ll r; ll cur; HashC(ll x, ll mod): x(x), mod(mod), f(0), r(0), cur(1) {} void update(char c) { f = (f*x)%mod; f = (f+c)%mod; ll d = (c * cur) % mod; cur = (cur*x)%mod; r = (r+d)%mod; } bool ok() { return f == r; } }; struct TwoBuffers { char* be; char* en; int b; int e; int size; int read; TwoBuffers(int size): be(new char[size]), en(new char[size]), size(size), b(0), e(0), read(0) {} void update(char c) { if(b < size) { be[b] = c; b++; } else { en[e] = c; e = (e+1)%size; } read++; } char fromBeginning(int i) { return be[i]; } char fromEnd(int i) { if(read >= 2*size) { return en[(e + 3*size - 1 - i) % size]; } else if(read >= size) { int k = e - i - 1; if(k < 0) { return be[k + size]; } else { return en[k]; } } else { return be[b - i - 1]; } } }; bool solve1() { char* buf = new char[TS + 117]; char c; int s = 0; for(;;) { c = getchar(); if(c < 'a' || c > 'z') { break; } buf[s] = c; s++; } int s2 = s/2; for(int i=0;i<s2;i++) { if(buf[i] != buf[s-1-i]) { return false; } } return true; } bool solve2() { TwoBuffers tb(131072); vector<HashC> hashes; hashes.push_back(HashC(951751951LL,1000000007LL)); hashes.push_back(HashC(0x1111BEEFLL,1013904223LL)); int hs = hashes.size(); char c; for(;;) { c = getchar(); if(c < 'a' || c > 'z') { break; } tb.update(c); for(int i=0;i<hs;i++) { hashes[i].update(c); } } bool ok = true; for(int i=0;i<hs;i++) { if(!hashes[i].ok()) { ok = false; break; } } if(ok) { int z = min(tb.size - 5, tb.read / 2); for(int i=0;i<z;i++) { if(tb.fromBeginning(i) != tb.fromEnd(i)) { ok = false; break; } } } return ok; } int main(int argc, char** argv) { int n; scanf("%d\n",&n); bool ok = false; if((n > 0) && (n <= TS)) { ok = solve1(); } else { ok = solve2(); } printf(ok?"TAK\n":"NIE\n"); return 0; } |