#include<cstdio> #include<vector> using namespace std; const int p=31; const long long q=1<<31-1; void hash_up(long long &h,int c, long long &pi){ h=(h+c*pi)%q; pi*=p; pi%=q; } void hash_down(long long &h, int c) { h*=p; h+=c; h%=q; } int main() { int n; scanf("%d",&n); vector<int> string; if(n==0) { int c=getchar(); while(true) { c=getchar(); if(c==10) break; string.push_back(c); } n=string.size(); int middle=n/2; long long h_left=0; long long h_right=0; long long pi=1; for(int i=0; i<n; ++i) { if(i<middle) { hash_up(h_left,string[i],pi); continue; } if(i==middle && n%2) continue; hash_down(h_right,string[i]); } if(h_left==h_right) printf("TAK\n"); else printf("NIE\n"); return 0; } int middle=n/2; long long h_left=0; long long h_right=0; long long pi=1; int c=getchar(); for(int i=0; i<n; ++i) { c=getchar(); if(i<middle) { hash_up(h_left,c,pi); continue; } if(i==middle && n%2) { continue; } hash_down(h_right,c); } if(h_left==h_right) 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 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 | #include<cstdio> #include<vector> using namespace std; const int p=31; const long long q=1<<31-1; void hash_up(long long &h,int c, long long &pi){ h=(h+c*pi)%q; pi*=p; pi%=q; } void hash_down(long long &h, int c) { h*=p; h+=c; h%=q; } int main() { int n; scanf("%d",&n); vector<int> string; if(n==0) { int c=getchar(); while(true) { c=getchar(); if(c==10) break; string.push_back(c); } n=string.size(); int middle=n/2; long long h_left=0; long long h_right=0; long long pi=1; for(int i=0; i<n; ++i) { if(i<middle) { hash_up(h_left,string[i],pi); continue; } if(i==middle && n%2) continue; hash_down(h_right,string[i]); } if(h_left==h_right) printf("TAK\n"); else printf("NIE\n"); return 0; } int middle=n/2; long long h_left=0; long long h_right=0; long long pi=1; int c=getchar(); for(int i=0; i<n; ++i) { c=getchar(); if(i<middle) { hash_up(h_left,c,pi); continue; } if(i==middle && n%2) { continue; } hash_down(h_right,c); } if(h_left==h_right) printf("TAK\n"); else printf("NIE\n"); return 0; } |