#include <cstdio> using namespace std; typedef long long LL; const LL base1 = 257, mod1 = 1e9+696969; const LL base2 = 997, mod2 = 1e9+7; int main() { int n; LL hash1 = 0, hash2 = 0; LL revHash1 = 0, revHash2 = 0, base1Pow = 1, base2Pow = 1; scanf("%d", &n); char c = getchar(); while(1) { c = getchar(); if(c < 'a' || c > 'z') break; int x = (int)(c-'a'); hash1 = (hash1*base1 + x) % mod1; hash2 = (hash2*base2 + x) % mod2; revHash1 = (revHash1 + x*base1Pow) % mod1; revHash2 = (revHash2 + x*base2Pow) % mod2; base1Pow = (base1Pow*base1) % mod1; base2Pow = (base2Pow*base2) % mod2; } if(hash1 == revHash1 && hash2 == revHash2) printf("TAK\n"); else printf("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 | #include <cstdio> using namespace std; typedef long long LL; const LL base1 = 257, mod1 = 1e9+696969; const LL base2 = 997, mod2 = 1e9+7; int main() { int n; LL hash1 = 0, hash2 = 0; LL revHash1 = 0, revHash2 = 0, base1Pow = 1, base2Pow = 1; scanf("%d", &n); char c = getchar(); while(1) { c = getchar(); if(c < 'a' || c > 'z') break; int x = (int)(c-'a'); hash1 = (hash1*base1 + x) % mod1; hash2 = (hash2*base2 + x) % mod2; revHash1 = (revHash1 + x*base1Pow) % mod1; revHash2 = (revHash2 + x*base2Pow) % mod2; base1Pow = (base1Pow*base1) % mod1; base2Pow = (base2Pow*base2) % mod2; } if(hash1 == revHash1 && hash2 == revHash2) printf("TAK\n"); else printf("NIE\n"); return 0; } |