#include <bits/stdc++.h> using namespace std; const int MOD[] = {1000000021, 1000000033}; const int BASE = 100003; struct Hash { int val[2]; Hash(int x = 0) { for (int i = 0; i < 2; i++) { val[i] = x % MOD[i]; } } }; Hash operator+(const Hash &a, const Hash &b) { Hash res; for (int i = 0; i < 2; i++) { res.val[i] = (a.val[i] + b.val[i]) % MOD[i]; } return res; } Hash operator*(const Hash &a, const Hash &b) { Hash res; for (int i = 0; i < 2; i++) { res.val[i] = ((long long) a.val[i] * b.val[i]) % MOD[i]; } return res; } bool operator==(const Hash &a, const Hash &b) { return make_pair(a.val[0], a.val[1]) == make_pair(b.val[0], b.val[1]); } int main() { ios_base::sync_with_stdio(0); int _; cin >> _; Hash hForward(0), hBackward(0), basePower(1); char c; while (cin >> c) { hForward = hForward * BASE + c; hBackward = hBackward + c * basePower; basePower = basePower * BASE; } cout << (hForward == hBackward ? "TAK" : "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 | #include <bits/stdc++.h> using namespace std; const int MOD[] = {1000000021, 1000000033}; const int BASE = 100003; struct Hash { int val[2]; Hash(int x = 0) { for (int i = 0; i < 2; i++) { val[i] = x % MOD[i]; } } }; Hash operator+(const Hash &a, const Hash &b) { Hash res; for (int i = 0; i < 2; i++) { res.val[i] = (a.val[i] + b.val[i]) % MOD[i]; } return res; } Hash operator*(const Hash &a, const Hash &b) { Hash res; for (int i = 0; i < 2; i++) { res.val[i] = ((long long) a.val[i] * b.val[i]) % MOD[i]; } return res; } bool operator==(const Hash &a, const Hash &b) { return make_pair(a.val[0], a.val[1]) == make_pair(b.val[0], b.val[1]); } int main() { ios_base::sync_with_stdio(0); int _; cin >> _; Hash hForward(0), hBackward(0), basePower(1); char c; while (cin >> c) { hForward = hForward * BASE + c; hBackward = hBackward + c * basePower; basePower = basePower * BASE; } cout << (hForward == hBackward ? "TAK" : "NIE"); return 0; } |