#include<iostream> #include<cstdio> long long p = 71; long long p_pow = 1; long long h_forward = 0; long long h_backward = 0; long long p2 = 37; long long p2_pow = 1; long long mod2 = 555555555555529ll; long long h2_forward = 0; long long h2_backward = 0; void skip() { int n; scanf("%d\n", &n); } void progress(int next_code) { h_forward = h_forward + next_code * p_pow; p_pow = p_pow * p; h_backward = (h_backward * p) + next_code; } void progress2(int next_code) { h2_forward = (h2_forward + next_code * p2_pow) % mod2; p2_pow = (p2_pow * p2) % mod2; h2_backward = ((h2_backward * p2) + next_code) % mod2; } bool is_palindrome() { return h_forward == h_backward && h2_forward == h2_backward; } bool is_char(char c) { return c >= 'a' && c <= 'z'; } void solve() { char c; while(is_char(c = getchar())) { int code = c - 'a' + 1; progress(code); progress2(code); } if (is_palindrome()) { std::cout << "TAK\n"; } else { std::cout << "NIE\n"; } } int main() { skip(); solve(); 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<iostream> #include<cstdio> long long p = 71; long long p_pow = 1; long long h_forward = 0; long long h_backward = 0; long long p2 = 37; long long p2_pow = 1; long long mod2 = 555555555555529ll; long long h2_forward = 0; long long h2_backward = 0; void skip() { int n; scanf("%d\n", &n); } void progress(int next_code) { h_forward = h_forward + next_code * p_pow; p_pow = p_pow * p; h_backward = (h_backward * p) + next_code; } void progress2(int next_code) { h2_forward = (h2_forward + next_code * p2_pow) % mod2; p2_pow = (p2_pow * p2) % mod2; h2_backward = ((h2_backward * p2) + next_code) % mod2; } bool is_palindrome() { return h_forward == h_backward && h2_forward == h2_backward; } bool is_char(char c) { return c >= 'a' && c <= 'z'; } void solve() { char c; while(is_char(c = getchar())) { int code = c - 'a' + 1; progress(code); progress2(code); } if (is_palindrome()) { std::cout << "TAK\n"; } else { std::cout << "NIE\n"; } } int main() { skip(); solve(); return 0; } |