#include<bits/stdc++.h> #define H1 1000000007 #define H2 1000696969 using namespace std; long long int n,h1,h2,h3,h4,h,e1=1,e2=1; char c; int main() { scanf("%lld", &n); while(scanf(" %c", &c) == 1) { h=e1*int(c); h1+=h; h2*=31; h2+=int(c); h=e2*int(c); h3+=h; h4*=31; h4+=int(c); e1*=31; e2*=31; h1%=H1; h2%=H1; e1%=H1; h3%=H2; h4%=H2; e2%=H2; } if(h1 == h2 && h3 == h4) { printf("TAK"); } else { printf("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 | #include<bits/stdc++.h> #define H1 1000000007 #define H2 1000696969 using namespace std; long long int n,h1,h2,h3,h4,h,e1=1,e2=1; char c; int main() { scanf("%lld", &n); while(scanf(" %c", &c) == 1) { h=e1*int(c); h1+=h; h2*=31; h2+=int(c); h=e2*int(c); h3+=h; h4*=31; h4+=int(c); e1*=31; e2*=31; h1%=H1; h2%=H1; e1%=H1; h3%=H2; h4%=H2; e2%=H2; } if(h1 == h2 && h3 == h4) { printf("TAK"); } else { printf("NIE"); } return 0; } |