#include <iostream> #define ull unsigned long long using namespace std; class DoubleOnlineHash { ull _mod, _base, _curr_base, _hash, _reversed_hash; ull charToUll(char c) { return 'z' - c; } public: DoubleOnlineHash(ull base, ull mod) : _mod(mod), _base(base), _curr_base(1), _hash(0), _reversed_hash(0) {} DoubleOnlineHash& operator+=(char c) { _hash *= _base; _hash += charToUll(c); _hash %= _mod; _reversed_hash += (_curr_base * charToUll(c)) % _mod; _reversed_hash %= _mod; _curr_base = (_curr_base * _base) % _mod; return *this; } ull get_hash() { return _hash; } ull get_reversed_hash() { return _reversed_hash; } }; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; DoubleOnlineHash h1(127, 1000000007), h2(137, 1000000009), h3(197, 982451653); char c; while (cin >> c) { h1 += c; h2 += c; h3 += c; } if (h1.get_hash() == h1.get_reversed_hash() && h2.get_hash() == h2.get_reversed_hash() && h3.get_hash() == h3.get_reversed_hash()) { cout << "TAK" << '\n'; } else { cout << "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 45 46 47 48 49 50 51 52 53 54 | #include <iostream> #define ull unsigned long long using namespace std; class DoubleOnlineHash { ull _mod, _base, _curr_base, _hash, _reversed_hash; ull charToUll(char c) { return 'z' - c; } public: DoubleOnlineHash(ull base, ull mod) : _mod(mod), _base(base), _curr_base(1), _hash(0), _reversed_hash(0) {} DoubleOnlineHash& operator+=(char c) { _hash *= _base; _hash += charToUll(c); _hash %= _mod; _reversed_hash += (_curr_base * charToUll(c)) % _mod; _reversed_hash %= _mod; _curr_base = (_curr_base * _base) % _mod; return *this; } ull get_hash() { return _hash; } ull get_reversed_hash() { return _reversed_hash; } }; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; DoubleOnlineHash h1(127, 1000000007), h2(137, 1000000009), h3(197, 982451653); char c; while (cin >> c) { h1 += c; h2 += c; h3 += c; } if (h1.get_hash() == h1.get_reversed_hash() && h2.get_hash() == h2.get_reversed_hash() && h3.get_hash() == h3.get_reversed_hash()) { cout << "TAK" << '\n'; } else { cout << "NIE" << '\n'; } } |