#include <bits/stdc++.h> using namespace std; char c; int n; const long long p=29,m=1000000009; long long cp=1,h1,h2; long long mod(int a) { if(a>=m) return a%m; else return a; } int main() { ios_base::sync_with_stdio(false); cin>>n; while(cin>>c) { c-='a'; h1=mod(h1+cp*c); h2=mod(h2*p+c); cp=mod(cp*p); } if(h1==h2) cout<<"TAK"; else cout<<"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 | #include <bits/stdc++.h> using namespace std; char c; int n; const long long p=29,m=1000000009; long long cp=1,h1,h2; long long mod(int a) { if(a>=m) return a%m; else return a; } int main() { ios_base::sync_with_stdio(false); cin>>n; while(cin>>c) { c-='a'; h1=mod(h1+cp*c); h2=mod(h2*p+c); cp=mod(cp*p); } if(h1==h2) cout<<"TAK"; else cout<<"NIE"; return 0; } |