#include <iostream> using namespace std; typedef long long LL; const LL BASE = 1000000007; const int p=53; LL hash1=0, hash2=0,pwr=1; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; char c; while(cin>>c) { hash1 = (hash1*p+c)%BASE; hash2 = (hash2+pwr*c)%BASE; pwr = (pwr*p)%BASE; //cout<<hash1<<" - "<<hash2<<endl; } if(hash1<0) hash1+=BASE; if(hash2<0) hash2+=BASE; cout<<((hash1==hash2)?"TAK":"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 | #include <iostream> using namespace std; typedef long long LL; const LL BASE = 1000000007; const int p=53; LL hash1=0, hash2=0,pwr=1; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; char c; while(cin>>c) { hash1 = (hash1*p+c)%BASE; hash2 = (hash2+pwr*c)%BASE; pwr = (pwr*p)%BASE; //cout<<hash1<<" - "<<hash2<<endl; } if(hash1<0) hash1+=BASE; if(hash2<0) hash2+=BASE; cout<<((hash1==hash2)?"TAK":"NIE"); return 0; } |