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
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>

using namespace std;

const int MAXN = 20000000;
const int WORDL = 40;
const int NORMAL_PALINDROM = 2500000; 
const unsigned int BIGQ = 4222302871;
const unsigned int BIGP = 907;

int n;
bool noraml_palindrom();
bool hash_palindrom();
bool heuristic_palindrom();

int main() {
    cin >> n;
    bool result;
    if (n == 0) {
        result = heuristic_palindrom();
    } else if (n <= 2 * NORMAL_PALINDROM) {
        result = noraml_palindrom();
    } else {
        result = hash_palindrom();
    }

    if (result == true) {
        cout << "TAK" << endl;
    }
    else {
        cout << "NIE" << endl;
    }
}

int to_number(const char &a, const char &b, const char &c, const char &d) {
    return (a - 'a') * 17576 + (b - 'a') * 676 + (c - 'a') * 26 + (d - 'a');
}

bool heuristic_palindrom() {
    const int BUFFOR = 2000;
    char * tab = new char[BUFFOR];
    const int FOURS_MAX = 456976 + 5;
    int * fours = new int[FOURS_MAX];
    for (int i = 0; i < FOURS_MAX; ++i) {
        fours[i] = 0;
    }
    int current = 0;
    bool atleast_three = false;
    while(cin >> tab[current]) {
        if (current + 1 == BUFFOR) {
            for (int i = 0; i <= BUFFOR - 4; ++i) {
                fours[to_number(tab[i], tab[i+1], tab[i+2], tab[i+3])]++;
            }
            tab[0] = tab[BUFFOR - 3];
            tab[1] = tab[BUFFOR - 2];
            tab[2] = tab[BUFFOR - 1];
            current = 3;
            atleast_three = true;
        } else {
            current++;
        }
    }
    if (current <= 4 && atleast_three == false) {
        if (current == 1) {
            return true;
        } else if (current == 2) {
            return tab[0] == tab[1];
        } else if (current == 3) {
            return tab[0] == tab[2];
        } else if (current == 4) { 
            return tab[0] == tab[3] && tab[1] == tab[2];
        } else {
            return false;
        }
    } else if (current >= 4) {
        for (int i = 0; i <= current - 4; ++i) {
            fours[to_number(tab[i], tab[i+1], tab[i+2], tab[i+3])]++;
        }
    }

    const char START = 'a';
    const char END = 'z';
    for (char a = START; a <= END; ++a) {
        for (char b = START; b <= END; ++b) {
            for (char c = START; c <= END; ++c) {
                for (char d = START; d <= END; ++d) {
                    if (fours[to_number(a,b,c,d)] != fours[to_number(d,c,b,a)]) {
                        return false;
                    }
                }
            }
        }    
    }
    delete [] tab;
    return true;
}

bool noraml_palindrom() {
    char * tab = new char[NORMAL_PALINDROM + 5];
    int i = 0;
    while (i < n / 2) {
        cin >> tab[i++];
    }
    if (n % 2 == 1) {
        char devnull;
        cin >> devnull;
    }
    char verify;
    while (i--) {
        cin >> verify;
        if (verify != tab[i]) {
            return false;
        }
    }
    delete[] tab;
    return true;
}

bool hash_palindrom() {

    unsigned int x = 2;
    unsigned long long y = 2;
    unsigned int * hashes = new unsigned int[MAXN / WORDL + 1];
    int hashesIdx = 0;
    int half = n / 2;
    int hash_end = half - half % WORDL;
    int i = 0;
    
    int partIdx = 0;
    char part[WORDL];
    while (i < hash_end) {
        cin >> part[partIdx++];
        if (partIdx == WORDL) {
            partIdx = 0;
            unsigned long long currHash = 0;
            for (int j = WORDL - 1; j >= 0; j--) {
                currHash = (currHash * BIGP + (part[j] - 'a')) % BIGQ;
            }
            hashes[hashesIdx++] = currHash;
            
        }
        i++;
    }
    char middle[WORDL];
    int m = 0;
    while (i < n / 2) {
        cin >> middle[m++];
        i++;
    }
    if (n % 2 == 1) {
        char devnull;
        cin >> devnull;
        i++;
    }
    char verifyMiddle;
    while (m--) {
        cin >> verifyMiddle;
        if (verifyMiddle != middle[m]) {
            return false;
        }
        i++;
    }
    int verPartIdx = 0;
    char verEnding;
    unsigned long long currHash = 0;
    while (i < n) {
        cin >> verEnding;
        currHash = (currHash * BIGP + (verEnding - 'a')) % BIGQ;
        verPartIdx++;
        if (verPartIdx == WORDL) {
            if (currHash != hashes[--hashesIdx]) {
                return false;
            }

            verPartIdx = 0;
            currHash = 0;
        }
        i++;
    }
    delete [] hashes;
    return true;
}