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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <queue>

#define NO_OF_CHARS 256
// p is a prime number used for evaluating Rabin Karp's Rolling hash
#define p 103
 
bool isPalindrome()
{
    int n = 0;
 
    char a;
    std::cin >> a;
    n++;
    int FR  = a % p;
    int S;

    a = getchar();
    if (a != EOF)
    {
        n++;
        S = a % p;
    }
    else
        return true;


    int h = 1;

    bool last = FR == S;
    char nextHalf = a;
    std::queue<char> half;
    half.push(a);
    while ((a=getchar()) != EOF) 
    {
        n++;
        if (half.size() < 1000000)
            half.push(a);

        if (n%2 == 1 && half.size()>0)
        {
            nextHalf = half.front();
            half.pop();
        }

        if (FR == S)
        {
            last = true;
            // std::cout << stream.str() << std::endl;
        }
        else
        {
            last = false;
        }
 
            if (n%2 == 0)
            {
                h = (h*NO_OF_CHARS) % p;
                FR  = (FR + h*nextHalf)%p;
                // std::cout << "update FR " << n-2 << " " << nextHalf << " " << a << std::endl; 
                S = (S*NO_OF_CHARS + a)%p;
            }
            else
            {
                S = (NO_OF_CHARS*(S + p - nextHalf*h)%p  + a)%p;
                // std::cout << "update S " << n-2 << " " << nextHalf << " " << a  << std::endl; 
            }

    }
    return FR==S;
}
 

int main()
{
    int n;
    std::cin >> n;
    bool is = isPalindrome();
    if (is)
        std::cout << "TAK" << std::endl;
    else
        std::cout << "NIE" << std::endl;
    return 0;
}