#include <iostream> const long long prime = 371424173; const long long base = rand() % prime; const long long prime2 = 798919897; const long long base2 = rand() % prime2; int main() { std::ios_base::sync_with_stdio(false); int n; std::cin >> n; long long hash = 0; long long rev_hash = 0; long long multiplier = 1; long long hash2 = 0; long long rev_hash2 = 0; long long multiplier2 = 1; char c; while (std::cin >> c) { hash += multiplier * c; hash %= prime; rev_hash *= base; rev_hash %= prime; rev_hash += c; multiplier *= base; multiplier %= prime; hash2 += multiplier2 * c; hash2 %= prime2; rev_hash2 *= base2; rev_hash2 %= prime2; rev_hash2 += c; multiplier2 *= base2; multiplier2 %= prime2; } rev_hash %= prime; rev_hash2 %= prime2; std::cout << (hash == rev_hash && hash2 == rev_hash2 ? "TAK\n" : "NIE\n"); 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 | #include <iostream> const long long prime = 371424173; const long long base = rand() % prime; const long long prime2 = 798919897; const long long base2 = rand() % prime2; int main() { std::ios_base::sync_with_stdio(false); int n; std::cin >> n; long long hash = 0; long long rev_hash = 0; long long multiplier = 1; long long hash2 = 0; long long rev_hash2 = 0; long long multiplier2 = 1; char c; while (std::cin >> c) { hash += multiplier * c; hash %= prime; rev_hash *= base; rev_hash %= prime; rev_hash += c; multiplier *= base; multiplier %= prime; hash2 += multiplier2 * c; hash2 %= prime2; rev_hash2 *= base2; rev_hash2 %= prime2; rev_hash2 += c; multiplier2 *= base2; multiplier2 %= prime2; } rev_hash %= prime; rev_hash2 %= prime2; std::cout << (hash == rev_hash && hash2 == rev_hash2 ? "TAK\n" : "NIE\n"); return 0; } |