#include <cstdio> typedef long long ll; struct hashv { int fi,se; hashv() {} hashv(int a,int b): fi(a),se(b) {} }; const ll mod1=1000000007; const ll mod2=1000000009; hashv operator + (hashv a,hashv b) { int c1=a.fi+b.fi,c2=a.se+b.se; if (c1>=mod1) c1-=mod1; if (c2>=mod2) c2-=mod2; return hashv(c1,c2); } hashv operator - (hashv a,hashv b) { int c1=a.fi-b.fi,c2=a.se-b.se; if (c1<0) c1+=mod1; if (c2<0) c2+=mod2; return hashv(c1,c2); } hashv operator * (hashv a,hashv b) { return hashv(1ll*a.fi*b.fi%mod1,1ll*a.se*b.se%mod2); } // head char ch; hashv pw,base,h1,h2; int main() { pw=hashv(1,1); base=hashv(23333,67777); h1=hashv(0,0); h2=hashv(0,0); for (ch=getchar();ch<'a'||ch>'z';ch=getchar()); while (1) { h1=h1*base+hashv(ch,ch); h2=h2+pw*hashv(ch,ch); pw=pw*base; ch=getchar(); if (ch<'a'||ch>'z') break; } if (h1.fi==h2.fi&&h1.se==h2.se) { puts("TAK"); } else { puts("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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #include <cstdio> typedef long long ll; struct hashv { int fi,se; hashv() {} hashv(int a,int b): fi(a),se(b) {} }; const ll mod1=1000000007; const ll mod2=1000000009; hashv operator + (hashv a,hashv b) { int c1=a.fi+b.fi,c2=a.se+b.se; if (c1>=mod1) c1-=mod1; if (c2>=mod2) c2-=mod2; return hashv(c1,c2); } hashv operator - (hashv a,hashv b) { int c1=a.fi-b.fi,c2=a.se-b.se; if (c1<0) c1+=mod1; if (c2<0) c2+=mod2; return hashv(c1,c2); } hashv operator * (hashv a,hashv b) { return hashv(1ll*a.fi*b.fi%mod1,1ll*a.se*b.se%mod2); } // head char ch; hashv pw,base,h1,h2; int main() { pw=hashv(1,1); base=hashv(23333,67777); h1=hashv(0,0); h2=hashv(0,0); for (ch=getchar();ch<'a'||ch>'z';ch=getchar()); while (1) { h1=h1*base+hashv(ch,ch); h2=h2+pw*hashv(ch,ch); pw=pw*base; ch=getchar(); if (ch<'a'||ch>'z') break; } if (h1.fi==h2.fi&&h1.se==h2.se) { puts("TAK"); } else { puts("NIE"); } } |