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
#include <iostream>
#include <vector>
using namespace std;

struct Hash {
    unsigned base, mod;
    unsigned value = 0;
    unsigned rev_value = 0;
    unsigned pow = 1;

    inline Hash(unsigned base_, unsigned mod_) : base(base_), mod(mod_) {}
    inline void update(unsigned i) {
        value = (value + pow * i) % mod;
        rev_value = (rev_value * base + i) % mod;
        pow = (pow * base) % mod;
    }
};

#define BUF_SIZE (1024*1024)
char buf[BUF_SIZE];

int main() {
    ios::sync_with_stdio(0);

    vector<Hash> hashes = {
        Hash(29, 133333357),
        Hash(31, 125000003),
        Hash(37, 105263161),
        Hash(41, 95238109),
        Hash(43, 90909101),
        Hash(47, 83333351),
        Hash(53, 74074093),
        Hash(59, 66666667),
        Hash(61, 64516141),
        Hash(67, 58823533),
        Hash(71, 55555559),
        Hash(73, 54054079),
        Hash(79, 50000017),
        Hash(83, 47619049),
        Hash(89, 44444453),
        // Hash(97, 40816387),
    };

    int n;
    cin >> n;
    while (cin.peek() < 'a' || cin.peek() > 'z') cin.get();
    while (1) {
        cin.read(buf, BUF_SIZE);
        size_t count = cin.gcount();
        if (count == 0) break;
        for (unsigned i = 0; i < count; ++i) {
            unsigned char c = buf[i];
            if (c < 'a' || c > 'z') break;
            for (Hash& h : hashes) {
                h.update((unsigned)c - 'a');
            }
        }
    }

    for (Hash& h : hashes) {
        if (h.value != h.rev_value) {
            cout << "NIE";
            return 0;
        }
    }
    cout << "TAK";
    return 0;
}