#include <bits/stdc++.h> #define mod1 ((long long)(1005858587)) #define mod2 ((long long)(1005858169)) #define mod3 ((long long)(420420587)) #define mod4 ((long long)(213769669)) #define mod5 ((long long)(213721373)) #define mod6 ((long long)(1000002173)) using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ long long fags[6] = {mod1,mod2,mod3,mod4,mod5,mod6}; long long multi[6] = {1, 1, 1, 1, 1, 1 }; long long hsh[6] = { 0, 0, 0, 0, 0, 0 }; long long revh[6] = { 0, 0, 0, 0, 0, 0 }; int main(int argc, char** argv) { int n; scanf("%d",&n); char c; while (cin>>c) { c = c-97; for (int i = 0; i < 6; i++) { hsh[i] = (hsh[i]*26+c)%fags[i]; revh[i] = (revh[i] + multi[i]*c)%fags[i]; multi[i] = (multi[i]*26)%fags[i]; } } bool dupy = false; for (int i = 0; i < 6; i++) if (hsh[i] != revh[i]) dupy = true; if (dupy) puts("NIE"); else puts("TAK"); 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 | #include <bits/stdc++.h> #define mod1 ((long long)(1005858587)) #define mod2 ((long long)(1005858169)) #define mod3 ((long long)(420420587)) #define mod4 ((long long)(213769669)) #define mod5 ((long long)(213721373)) #define mod6 ((long long)(1000002173)) using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ long long fags[6] = {mod1,mod2,mod3,mod4,mod5,mod6}; long long multi[6] = {1, 1, 1, 1, 1, 1 }; long long hsh[6] = { 0, 0, 0, 0, 0, 0 }; long long revh[6] = { 0, 0, 0, 0, 0, 0 }; int main(int argc, char** argv) { int n; scanf("%d",&n); char c; while (cin>>c) { c = c-97; for (int i = 0; i < 6; i++) { hsh[i] = (hsh[i]*26+c)%fags[i]; revh[i] = (revh[i] + multi[i]*c)%fags[i]; multi[i] = (multi[i]*26)%fags[i]; } } bool dupy = false; for (int i = 0; i < 6; i++) if (hsh[i] != revh[i]) dupy = true; if (dupy) puts("NIE"); else puts("TAK"); return 0; } |