#include<bits/stdc++.h> #define ull unsigned long long #define ll long long using namespace std; const ll mod=1e9+7; int main(){ int n,i,p=313,p2=2137; char c; cin >>n; scanf("%c",&c); ull h1=0,h2=0,pot=1; ll hh1=0,hh2=0,pp=1; while(scanf("%c",&c)){ if(c<'a' || c>'z'){ break; } h1=h1*p+c; h2=h2+c*pot; pot=pot*p; hh1=(hh1*p2+c)%mod; hh2=(hh2+pp*c)%mod; pp*=p2; pp%=mod; } if(h1==h2 && hh1==hh2){ cout <<"TAK"; } else{ cout <<"NIE"; } }
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 | #include<bits/stdc++.h> #define ull unsigned long long #define ll long long using namespace std; const ll mod=1e9+7; int main(){ int n,i,p=313,p2=2137; char c; cin >>n; scanf("%c",&c); ull h1=0,h2=0,pot=1; ll hh1=0,hh2=0,pp=1; while(scanf("%c",&c)){ if(c<'a' || c>'z'){ break; } h1=h1*p+c; h2=h2+c*pot; pot=pot*p; hh1=(hh1*p2+c)%mod; hh2=(hh2+pp*c)%mod; pp*=p2; pp%=mod; } if(h1==h2 && hh1==hh2){ cout <<"TAK"; } else{ cout <<"NIE"; } } |