//Damian Denc
#include<bits/stdc++.h>
using namespace std;
const unsigned int mod1 = 1000000007; // 1,000,000,007
const unsigned int mod2 = 1000696969; // 1,000,696,969
const unsigned int p = 29;
unsigned long long pom1 = 1, pom2 = 1;
unsigned long long H[4];
int main()
{
char tmp;
scanf("%*d ");
while((tmp=getchar()) > 96)
{
H[0]=H[0]+((tmp-97)*pom1);
H[2]=H[2]+((tmp-97)*pom2);
H[0]%=mod1;
H[2]%=mod2;
H[1]*=p;
H[1]+=tmp-97;
H[3]*=p;
H[3]+=tmp-97;
H[1]%=mod1;
H[3]%=mod2;
pom1%=mod1;
pom2%=mod2;
pom1*=p;
pom2*=p;
}
if(H[0]==H[1] && H[2]==H[3])
printf("TAK");
else
printf("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 31 32 33 34 35 36 37 38 39 40 41 42 | //Damian Denc #include<bits/stdc++.h> using namespace std; const unsigned int mod1 = 1000000007; // 1,000,000,007 const unsigned int mod2 = 1000696969; // 1,000,696,969 const unsigned int p = 29; unsigned long long pom1 = 1, pom2 = 1; unsigned long long H[4]; int main() { char tmp; scanf("%*d "); while((tmp=getchar()) > 96) { H[0]=H[0]+((tmp-97)*pom1); H[2]=H[2]+((tmp-97)*pom2); H[0]%=mod1; H[2]%=mod2; H[1]*=p; H[1]+=tmp-97; H[3]*=p; H[3]+=tmp-97; H[1]%=mod1; H[3]%=mod2; pom1%=mod1; pom2%=mod2; pom1*=p; pom2*=p; } if(H[0]==H[1] && H[2]==H[3]) printf("TAK"); else printf("NIE"); return 0; } |
English