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
#include <bits/stdc++.h>

#define DEBUG if (0)
#define COUT cout << "\e[36m"
#define VAR(v) " [\e[32m" << #v << "\e[36m=\e[91m" << v << "\e[36m] "
#define ENDL "\e[39m" << endl

using namespace std;

struct Hash
{
    long long toleft = 0, toright = 0;
    long long k, P, currk = 1, currP = 1;

    void addchar(char a)
    {
        DEBUG COUT << "PRZED: " << (int)a << " PO: ";
        a -= 'a' -1;
        DEBUG COUT << (int)a << ENDL;
        toright = (toright+(long long)a*currk)%P;
        toleft = (toleft*k+(long long)a)%P;
        currk = (currk*k)%P;
    }

    Hash(long long ka, long long Pe)
    {
        k = ka;
        P = Pe;
    }
};

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, n2 = 0;
    cin >> n;

    Hash hash1(67, 1000000123);
    Hash hash2(89, 1000000087);
    Hash hash3(277, 1000000181);


    char a;
    //a =getchar();
    DEBUG COUT << VAR(a) << VAR((int)a) << ENDL;
    while(cin >> a)
    {
        if(0 && a == '\n')
        {
            a = getchar();
            continue;
        }
        n2++;

        DEBUG COUT << VAR(a) << ENDL;
        hash1.addchar(a);
        hash2.addchar(a);
        if(n2 <= 1e6 || n <= 1e6)
            hash2.addchar(a);
        //a = getchar();
    }
    if(hash1.toright == hash1.toleft && hash2.toright == hash2.toleft && ((n > 1e6 || n2 > 1e6) ||  hash3.toright == hash3.toleft))
    {
        cout << "TAK\n";
    }
    else
    {
        cout << "NIE\n";
    }

    return 0;
}