#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL Primes[] = {1000000007LL, 1000000009LL, 1000000021LL};
struct Hash
{
LL M, B;
LL h1, h2;
LL power;
Hash () {}
Hash (int i)
{
M = Primes[i];
B = rand() % 2000LL + 1000LL;
if (!(B & 1LL))
++B;
h1 = h2 = 0LL;
power = 1LL;
}
void Add (char c)
{
h1 = (h1 * B + c) % M;
h2 = (h2 + power * c) % M;
power = (power * B) % M;
}
bool Equals ()
{
return h1 == h2;
}
};
int main ()
{
srand(time(NULL));
char c = getchar_unlocked();
while (c < 'a' || c > 'z')
c = getchar_unlocked();
Hash H[3];
for (int i = 0; i < 3; ++i)
H[i] = Hash(i);
while (true)
{
for (int i = 0; i < 3; ++i)
H[i].Add(c);
c = getchar_unlocked();
if (c < 'a' || c > 'z')
break;
}
if (H[0].Equals() && H[1].Equals() && H[2].Equals())
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include <bits/stdc++.h> using namespace std; typedef long long LL; const LL Primes[] = {1000000007LL, 1000000009LL, 1000000021LL}; struct Hash { LL M, B; LL h1, h2; LL power; Hash () {} Hash (int i) { M = Primes[i]; B = rand() % 2000LL + 1000LL; if (!(B & 1LL)) ++B; h1 = h2 = 0LL; power = 1LL; } void Add (char c) { h1 = (h1 * B + c) % M; h2 = (h2 + power * c) % M; power = (power * B) % M; } bool Equals () { return h1 == h2; } }; int main () { srand(time(NULL)); char c = getchar_unlocked(); while (c < 'a' || c > 'z') c = getchar_unlocked(); Hash H[3]; for (int i = 0; i < 3; ++i) H[i] = Hash(i); while (true) { for (int i = 0; i < 3; ++i) H[i].Add(c); c = getchar_unlocked(); if (c < 'a' || c > 'z') break; } if (H[0].Equals() && H[1].Equals() && H[2].Equals()) printf("TAK"); else printf("NIE"); return 0; } |
English