#include<iostream> #include<cstdio> using namespace std; long long n,l,ll,r,rr,P=29,P2=29,M=1e12+39,M2=1e11+57; int a; int main() { cin>>n; getchar(); while(1) { a=getchar()-96; if(a<1||a>26)break; l+=a*P%M;ll+=a*P2%M2; l%=M;ll%=M2; r+=a;rr+=a; r*=29;rr*=29; r%=M;rr%=M2; P=P*29%M;P2=P2*29%M2; } if(r==l&&rr==ll)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 | #include<iostream> #include<cstdio> using namespace std; long long n,l,ll,r,rr,P=29,P2=29,M=1e12+39,M2=1e11+57; int a; int main() { cin>>n; getchar(); while(1) { a=getchar()-96; if(a<1||a>26)break; l+=a*P%M;ll+=a*P2%M2; l%=M;ll%=M2; r+=a;rr+=a; r*=29;rr*=29; r%=M;rr%=M2; P=P*29%M;P2=P2*29%M2; } if(r==l&&rr==ll)cout<<"TAK"; else cout<<"NIE"; } |