#include <cstdio> using namespace std; char s[3000002]; int pairs[26][26]; int counts[26]; bool basicAlgo(int n) { char c; for (int i = 0; i < n / 2; i++) { s[i] = getchar(); } if (n % 2 == 1) { c = getchar(); } for (int i = (n + 1) / 2; i < n; i++) { c = getchar(); if (c != s[n - i - 1]) { return false; } } return true; } void setBit(int k) { s[k / 8] |= (1 << (7 - k % 8)); } bool getBit(int k) { return s[k / 8] & (1 << (7 - k % 8)); } bool bitsEqual(int k, int l) { return getBit(k) == getBit(l); } bool extendedAlgo() { char c0; setBit(0); int n = 1; char c1 = c0 = getchar(); char c2 = getchar(); counts[c0 - 'a']++; while (c2 >= 'a' && c2 <= 'z') { pairs[c1 - 'a'][c2 - 'a']++; counts[c2 - 'a']++; if (c2 == c0) { setBit(n); } n++; c1 = c2; c2 = getchar(); } int odds = 0; for (int count : counts) { if (count % 2 == 1) { odds++; } } if (n % 2 == 0 && odds != 0) { return false; } if (n % 2 == 1 && odds != 1) { return false; } for (int i = 0; i < 26; i++) { for (int j = 0; j < 26; j++) { if (pairs[i][j] != pairs[j][i]) { return false; } } } for (int i = 0; i < n / 2; i++) { if (!bitsEqual(i, n - i - 1)) { return false; } } return true; } int main() { int n; scanf("%d\n", &n); bool isPalin; if (n > 0 && n <= 6000000) { isPalin = basicAlgo(n); } else { isPalin = extendedAlgo(); } printf("%s\n", isPalin ? "TAK" : "NIE"); 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #include <cstdio> using namespace std; char s[3000002]; int pairs[26][26]; int counts[26]; bool basicAlgo(int n) { char c; for (int i = 0; i < n / 2; i++) { s[i] = getchar(); } if (n % 2 == 1) { c = getchar(); } for (int i = (n + 1) / 2; i < n; i++) { c = getchar(); if (c != s[n - i - 1]) { return false; } } return true; } void setBit(int k) { s[k / 8] |= (1 << (7 - k % 8)); } bool getBit(int k) { return s[k / 8] & (1 << (7 - k % 8)); } bool bitsEqual(int k, int l) { return getBit(k) == getBit(l); } bool extendedAlgo() { char c0; setBit(0); int n = 1; char c1 = c0 = getchar(); char c2 = getchar(); counts[c0 - 'a']++; while (c2 >= 'a' && c2 <= 'z') { pairs[c1 - 'a'][c2 - 'a']++; counts[c2 - 'a']++; if (c2 == c0) { setBit(n); } n++; c1 = c2; c2 = getchar(); } int odds = 0; for (int count : counts) { if (count % 2 == 1) { odds++; } } if (n % 2 == 0 && odds != 0) { return false; } if (n % 2 == 1 && odds != 1) { return false; } for (int i = 0; i < 26; i++) { for (int j = 0; j < 26; j++) { if (pairs[i][j] != pairs[j][i]) { return false; } } } for (int i = 0; i < n / 2; i++) { if (!bitsEqual(i, n - i - 1)) { return false; } } return true; } int main() { int n; scanf("%d\n", &n); bool isPalin; if (n > 0 && n <= 6000000) { isPalin = basicAlgo(n); } else { isPalin = extendedAlgo(); } printf("%s\n", isPalin ? "TAK" : "NIE"); return 0; } |