#include <iostream> #include <array> constexpr size_t MAX_ARR = 1000000; constexpr size_t LETTERS = 'z' - 'a' + 1; constexpr unsigned long long MULT_ONE = 1103515245; int main() { size_t n; std::cin >> n; std::array<char, MAX_ARR> basic; basic.fill(0); int counts[LETTERS][LETTERS] = {}; unsigned long long hash0 = 0, hash1 = 0, mult1 = 1; char ch, prev; size_t i = 0; while (std::cin >> ch) { ch = ch - 'a'; basic[i % MAX_ARR] ^= ch; if (i != 0) { counts[prev][ch]++; } prev = ch; hash0 *= MULT_ONE; hash0 += ch; hash1 += ch * mult1; mult1 *= MULT_ONE; ++i; } bool ok = hash0 == hash1; for (size_t j = 0, k = (i - 1) % MAX_ARR; j < k; ++j, --k) { ok &= basic[j] == basic[k]; } if (i > MAX_ARR && i % MAX_ARR != 0) { for (size_t j = i % MAX_ARR, k = MAX_ARR - 1; j < k; ++j, --k) { ok &= basic[j] == basic[k]; } } for (size_t j = 0; j + 1 < LETTERS; ++j) { for (size_t k = j + 1; k < LETTERS; ++k) { ok &= counts[j][k] == counts[k][j]; } } std::cout << (ok ? "TAK" : "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 54 55 56 57 58 59 | #include <iostream> #include <array> constexpr size_t MAX_ARR = 1000000; constexpr size_t LETTERS = 'z' - 'a' + 1; constexpr unsigned long long MULT_ONE = 1103515245; int main() { size_t n; std::cin >> n; std::array<char, MAX_ARR> basic; basic.fill(0); int counts[LETTERS][LETTERS] = {}; unsigned long long hash0 = 0, hash1 = 0, mult1 = 1; char ch, prev; size_t i = 0; while (std::cin >> ch) { ch = ch - 'a'; basic[i % MAX_ARR] ^= ch; if (i != 0) { counts[prev][ch]++; } prev = ch; hash0 *= MULT_ONE; hash0 += ch; hash1 += ch * mult1; mult1 *= MULT_ONE; ++i; } bool ok = hash0 == hash1; for (size_t j = 0, k = (i - 1) % MAX_ARR; j < k; ++j, --k) { ok &= basic[j] == basic[k]; } if (i > MAX_ARR && i % MAX_ARR != 0) { for (size_t j = i % MAX_ARR, k = MAX_ARR - 1; j < k; ++j, --k) { ok &= basic[j] == basic[k]; } } for (size_t j = 0; j + 1 < LETTERS; ++j) { for (size_t k = j + 1; k < LETTERS; ++k) { ok &= counts[j][k] == counts[k][j]; } } std::cout << (ok ? "TAK" : "NIE") << "\n"; } |