#include <iostream> using namespace std; typedef long long ll; const ll p[] = {383, 317}; const int m = 1e9 + 17; ll ip[] = {1, 1}; int N; char z; pair<ll, ll> hasz[2]; int main() { ios_base::sync_with_stdio(NULL); cout.tie(NULL); cin.tie(NULL); cin >> N; while(cin >> z) { for(int k = 0; k < 2; ++k) hasz[k].first = (hasz[k].first * p[k] + z) % m, hasz[k].second = (hasz[k].second + ((ll)z * ip[k])) % m; for(int k = 0; k < 2; ++k) ip[k] = (ip[k] * p[k]) % m; } if(hasz[0].first == hasz[0].second && hasz[1].first == hasz[1].first) cout << "TAK" << endl; else cout << "NIE" << endl; 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 | #include <iostream> using namespace std; typedef long long ll; const ll p[] = {383, 317}; const int m = 1e9 + 17; ll ip[] = {1, 1}; int N; char z; pair<ll, ll> hasz[2]; int main() { ios_base::sync_with_stdio(NULL); cout.tie(NULL); cin.tie(NULL); cin >> N; while(cin >> z) { for(int k = 0; k < 2; ++k) hasz[k].first = (hasz[k].first * p[k] + z) % m, hasz[k].second = (hasz[k].second + ((ll)z * ip[k])) % m; for(int k = 0; k < 2; ++k) ip[k] = (ip[k] * p[k]) % m; } if(hasz[0].first == hasz[0].second && hasz[1].first == hasz[1].first) cout << "TAK" << endl; else cout << "NIE" << endl; return 0; } |