#pragma region Template #include <bits/stdc++.h> using namespace std; #define For(i, n) for (int i = 0; i < (n); i++) #define ForD(i, n) for (int i = (n) - 1; i >= 0; i--) #define SORT(x) sort(begin(x), end(x)) #define REP(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #if DEBUG #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string>) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } #else #define error(...) do {} while (0) #endif #define _upgrade do { ios::sync_with_stdio(0); cin.tie(0); } while (0) typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #pragma endregion vector<ll> hash_base = { 29, 37, 41, 47, 53 }; auto hash_multed = hash_base; vector<ll> hash_for = {0, 0, 0, 0, 0}; vector<ll> hash_rev = {0, 0, 0, 0, 0}; const ll MOD = 1000000021; int main() { _upgrade; int n; cin >> n; char c; while (cin >> c) { ll x = c - 'a' + 1; For (i, (int)hash_for.size()) { hash_for[i] = (hash_for[i] + x * hash_multed[i]) % MOD; hash_rev[i] = ((hash_rev[i] * hash_base[i]) % MOD + x * hash_base[i]) % MOD; hash_multed[i] = (hash_multed[i] * hash_base[i]) % MOD; } } For (i, (int)hash_for.size()) { if (hash_rev[i] != hash_for[i]) { cout << "NIE\n"; return 0; } } cout << "TAK\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 55 56 57 58 59 60 61 62 63 64 65 | #pragma region Template #include <bits/stdc++.h> using namespace std; #define For(i, n) for (int i = 0; i < (n); i++) #define ForD(i, n) for (int i = (n) - 1; i >= 0; i--) #define SORT(x) sort(begin(x), end(x)) #define REP(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #if DEBUG #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string>) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } #else #define error(...) do {} while (0) #endif #define _upgrade do { ios::sync_with_stdio(0); cin.tie(0); } while (0) typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #pragma endregion vector<ll> hash_base = { 29, 37, 41, 47, 53 }; auto hash_multed = hash_base; vector<ll> hash_for = {0, 0, 0, 0, 0}; vector<ll> hash_rev = {0, 0, 0, 0, 0}; const ll MOD = 1000000021; int main() { _upgrade; int n; cin >> n; char c; while (cin >> c) { ll x = c - 'a' + 1; For (i, (int)hash_for.size()) { hash_for[i] = (hash_for[i] + x * hash_multed[i]) % MOD; hash_rev[i] = ((hash_rev[i] * hash_base[i]) % MOD + x * hash_base[i]) % MOD; hash_multed[i] = (hash_multed[i] * hash_base[i]) % MOD; } } For (i, (int)hash_for.size()) { if (hash_rev[i] != hash_for[i]) { cout << "NIE\n"; return 0; } } cout << "TAK\n"; } |