#include <bits/stdc++.h> using namespace std; using ll = long long; struct Hash { int P, M; ll h, h_rev; ll p_n; Hash(int P_, int M_) { P = P_; M = M_; h = h_rev = 0; p_n = 1; } void append(char c) { int x = c - 'a' + 37; h_rev = (h_rev * P + x) % M; h = (h + x * p_n) % M; p_n = (p_n * P) % M; } bool is_pal() { return h == h_rev; } }; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; char a; Hash h1(1039, 1222111333), h2(1003, 1100000017); while (cin >> a) { if ('a' <= a && a <= 'z') { h1.append(a); h2.append(a); } } if (h1.is_pal() && h2.is_pal()) { 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 | #include <bits/stdc++.h> using namespace std; using ll = long long; struct Hash { int P, M; ll h, h_rev; ll p_n; Hash(int P_, int M_) { P = P_; M = M_; h = h_rev = 0; p_n = 1; } void append(char c) { int x = c - 'a' + 37; h_rev = (h_rev * P + x) % M; h = (h + x * p_n) % M; p_n = (p_n * P) % M; } bool is_pal() { return h == h_rev; } }; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; char a; Hash h1(1039, 1222111333), h2(1003, 1100000017); while (cin >> a) { if ('a' <= a && a <= 'z') { h1.append(a); h2.append(a); } } if (h1.is_pal() && h2.is_pal()) { cout << "TAK\n"; } else { cout << "NIE\n"; } } |