#include <cstdio> #include <cstdlib> #include <cassert> #define SZ 2 unsigned int hashes[SZ]; unsigned int bashes[SZ]; unsigned long long bases[SZ]; unsigned int powers[SZ]; unsigned int modulos[SZ]; int main() { int n; scanf("%d", &n); char c; scanf("%c", &c); assert(c == '\n'); for (int i=0; i<SZ; i++) { bases[i] = rand() % (1ll<<31); modulos[i] = rand() % (1ll<<31); powers[i] = 1; } while (EOF != scanf("%c", &c)) { if (c == '\n') break; for (int i=0; i<SZ; i++) { hashes[i] = (hashes[i] + c * (unsigned long long)powers[i]) % modulos[i]; powers[i] = (powers[i] * (unsigned long long)bases[i]) % modulos[i]; bashes[i] = (bashes[i] * (unsigned long long)bases[i] + c) % modulos[i]; } } bool eq = true; for (int i=0; i<SZ; i++) { if (hashes[i] != bashes[i]) eq = false; } if (eq) { printf("TAK\n"); } else { printf("NIE\n"); } }
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 | #include <cstdio> #include <cstdlib> #include <cassert> #define SZ 2 unsigned int hashes[SZ]; unsigned int bashes[SZ]; unsigned long long bases[SZ]; unsigned int powers[SZ]; unsigned int modulos[SZ]; int main() { int n; scanf("%d", &n); char c; scanf("%c", &c); assert(c == '\n'); for (int i=0; i<SZ; i++) { bases[i] = rand() % (1ll<<31); modulos[i] = rand() % (1ll<<31); powers[i] = 1; } while (EOF != scanf("%c", &c)) { if (c == '\n') break; for (int i=0; i<SZ; i++) { hashes[i] = (hashes[i] + c * (unsigned long long)powers[i]) % modulos[i]; powers[i] = (powers[i] * (unsigned long long)bases[i]) % modulos[i]; bashes[i] = (bashes[i] * (unsigned long long)bases[i] + c) % modulos[i]; } } bool eq = true; for (int i=0; i<SZ; i++) { if (hashes[i] != bashes[i]) eq = false; } if (eq) { printf("TAK\n"); } else { printf("NIE\n"); } } |