#include <iostream> #include <stdio.h> using namespace std; const long long inf=1000000007,P1=101,P2=107; int main(){ long long h11=0,h12=0,pom1=1,h21=0,h22=0,pom2=1; int n; char c; cin >> n; if(n!=0){ while(n--){ cin >> c; h11*=P1; h11+=c-'a'; h12+=pom1*(c-'a'); pom1*=P1; h21*=P2; h21+=c-'a'; h22+=pom2*(c-'a'); pom2*=P2; h11%=inf; h12%=inf; pom1%=inf; h21%=inf; h22%=inf; pom2%=inf; } if(h11==h12&&h21==h22)cout<<"TAK"<<endl; else cout<<"NIE"<<endl; } else{ cin >> c; do{ h11*=P1; h11+=c-'a'; h12+=pom1*(c-'a'); pom1*=P1; h21*=P2; h21+=c-'a'; h22+=pom2*(c-'a'); pom2*=P2; h11%=inf; h12%=inf; pom1%=inf; h21%=inf; h22%=inf; pom2%=inf; c=getchar(); }while(c!='\n'); if(h11==h12&&h21==h22)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 51 52 53 54 55 56 | #include <iostream> #include <stdio.h> using namespace std; const long long inf=1000000007,P1=101,P2=107; int main(){ long long h11=0,h12=0,pom1=1,h21=0,h22=0,pom2=1; int n; char c; cin >> n; if(n!=0){ while(n--){ cin >> c; h11*=P1; h11+=c-'a'; h12+=pom1*(c-'a'); pom1*=P1; h21*=P2; h21+=c-'a'; h22+=pom2*(c-'a'); pom2*=P2; h11%=inf; h12%=inf; pom1%=inf; h21%=inf; h22%=inf; pom2%=inf; } if(h11==h12&&h21==h22)cout<<"TAK"<<endl; else cout<<"NIE"<<endl; } else{ cin >> c; do{ h11*=P1; h11+=c-'a'; h12+=pom1*(c-'a'); pom1*=P1; h21*=P2; h21+=c-'a'; h22+=pom2*(c-'a'); pom2*=P2; h11%=inf; h12%=inf; pom1%=inf; h21%=inf; h22%=inf; pom2%=inf; c=getchar(); }while(c!='\n'); if(h11==h12&&h21==h22)cout<<"TAK"<<endl; else cout<<"NIE"<<endl; } } |