#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n; char c;
cin >> n;
cin.get();
ll hash_from_left[3] = {0, 0, 0}, hash_from_right[3] = {0, 0, 0};
ll x[3] = {1, 1, 1}, p[3] = {20000003, 20000023, 20000033}, q[3] = {1000000007, 1000000009, 1000000009};
while (true) {
c = cin.get();
if (c == '\n')
break;
for (int i = 0; i < 2; i++) {
hash_from_left[i] += x[i] * c;
hash_from_left[i] %= q[i];
hash_from_right[i] *= p[i];
hash_from_right[i] += c;
hash_from_right[i] %= q[i];
x[i] *= p[i];
x[i] %= q[i];
}
}
if (hash_from_left[0] == hash_from_right[0] && hash_from_left[1] == hash_from_right[1] && hash_from_left[2] == hash_from_right[2])
cout << "TAK\n";
else
cout << "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 | #include <bits/stdc++.h> #define ll long long using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n; char c; cin >> n; cin.get(); ll hash_from_left[3] = {0, 0, 0}, hash_from_right[3] = {0, 0, 0}; ll x[3] = {1, 1, 1}, p[3] = {20000003, 20000023, 20000033}, q[3] = {1000000007, 1000000009, 1000000009}; while (true) { c = cin.get(); if (c == '\n') break; for (int i = 0; i < 2; i++) { hash_from_left[i] += x[i] * c; hash_from_left[i] %= q[i]; hash_from_right[i] *= p[i]; hash_from_right[i] += c; hash_from_right[i] %= q[i]; x[i] *= p[i]; x[i] %= q[i]; } } if (hash_from_left[0] == hash_from_right[0] && hash_from_left[1] == hash_from_right[1] && hash_from_left[2] == hash_from_right[2]) cout << "TAK\n"; else cout << "NIE\n"; return 0; } |
English