#include <iostream> #include <vector> #include <algorithm> using namespace std; const long long MOD[3]={1000696969, 1000000009, 2038074751}; const long long P[3]={29, 31, 101}; long long Hash[3]; long long HashR[3]; long long p[3]; long long getH(long long Hash, long long p, long long x, long long MOD){ return (Hash+(x*p)%MOD)%MOD; } long long getHR(long long Hash, long long p, long long x, long long MOD){ return (((Hash+x)%MOD)*p)%MOD; } int main(){ char c; int n; cin>>n; p[0]=P[0]; p[1]=P[1]; p[2]=P[2]; while(cin>>c){ long long x=c-'a'; for(int i=0; i<3; i++){ Hash[i]=getH(Hash[i], p[i], x, MOD[i]); HashR[i]=getHR(HashR[i], P[i], x, MOD[i]); p[i]=(p[i]*P[i])%MOD[i]; } } bool odp=true; for(int i=0; i<3; i++){ //cout<<Hash[i]<<" "<<HashR[i]<<endl; if(Hash[i]!=HashR[i]) odp=false; } if(odp) cout<<"TAK"<<endl; else cout<<"NIE"<<endl; }
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; const long long MOD[3]={1000696969, 1000000009, 2038074751}; const long long P[3]={29, 31, 101}; long long Hash[3]; long long HashR[3]; long long p[3]; long long getH(long long Hash, long long p, long long x, long long MOD){ return (Hash+(x*p)%MOD)%MOD; } long long getHR(long long Hash, long long p, long long x, long long MOD){ return (((Hash+x)%MOD)*p)%MOD; } int main(){ char c; int n; cin>>n; p[0]=P[0]; p[1]=P[1]; p[2]=P[2]; while(cin>>c){ long long x=c-'a'; for(int i=0; i<3; i++){ Hash[i]=getH(Hash[i], p[i], x, MOD[i]); HashR[i]=getHR(HashR[i], P[i], x, MOD[i]); p[i]=(p[i]*P[i])%MOD[i]; } } bool odp=true; for(int i=0; i<3; i++){ //cout<<Hash[i]<<" "<<HashR[i]<<endl; if(Hash[i]!=HashR[i]) odp=false; } if(odp) cout<<"TAK"<<endl; else cout<<"NIE"<<endl; } |