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

#define forn(i, n) for(int i = 0; i < n; ++i)

using namespace std;

typedef long long ll;
typedef pair<int, int> pint;

ll cnt[26];

vector<int> h; 
vector<int> ih; 

const ll interval = 349;
const ll p = 17;
vector<ll> ppows;
const ll mod = 982451653;

ll primes[10] = {29,31,37,41,43,47,53,59,61,67};
ll pows[10] = {1,1,1,1,1,1,1,1,1,1};
ll mods[10] = {32416187567LL, 32416188223LL, 32416188809LL, 32416189391LL, 32416187627LL, 32416188227LL, 32416188839LL, 32416189459LL, 32416187651LL, 32416188241LL};
ll hash_[10];
ll invhash[10];

void init()
{
    for (int i=0; i<10; i++)
    {
        hash_[i] = 0;
        invhash[i] = 0;
    }

    for (int i=0; i<26; i++)
    {
        cnt[i] = 0;
    }

    ppows.push_back(1);
    for (int i=0; i<interval-1; i++)
    {
        ll ppow = (ppows.back() * p) % mod;
        ppows.push_back(ppow);
    }
}

string solve_small(int n)
{
    string s;
    cin >> s;
    for (int i=0; i<s.size(); i++)
    {
        if (s[i] != s[s.size()-1-i])
            return "NIE";
    }
    return "TAK";
}

void update_tabs(char c)
{
    cnt[c-'a']++;
}

void update_hashes(char c)
{
    for (int i=0; i<10; i++)
    {
        hash_[i] = (hash_[i] + ( (c-'a'+1) * pows[i] ) % mods[i] ) % mods[i];
        pows[i] = (pows[i] * primes[i]) % mods[i];

        invhash[i] = ( ( invhash[i] * primes[i] ) % mods[i] + (c-'a'+1) ) % mods[i];
    }
}

void update_h(char c, int i, int rest)
{
    int pos = i / interval;
    h[pos] = ( h[pos] + ( (c-'a'+1)*ppows[i % interval] ) % mod ) % mod;

    int ipos = (rest > 0 ? (i + (interval - rest))/interval : pos);
    ih[ipos] = ( ( ih[ipos] * p ) % mod + (c-'a'+1) ) % mod;
}

bool check_hashes()
{
    for (int i=0; i<10; i++)
    {
        if (hash_[i] != invhash[i])
            return false;
    }
    return true;
}

bool check_tabs()
{
    int odd = 0;
    for (int i=0; i<26; i++)
        odd += (cnt[i]%2);

    if (odd > 1)
        return false;

    return true;
}

bool check_h(int n)
{
    int lastpos = (n - 1) / interval; 
    for (int i=0; i<=lastpos; i++)
    {
        if (h[i] != ih[lastpos-i])
            return false;
    }

    return true;
}

string solven(int n)
{
    h.resize(60042,0);
    ih.resize(60042,0);

    char c;
    int rest = n % interval;
    for (int i=0; i<n; i++)
    {
        cin >> c;
        update_tabs(c);
        update_hashes(c);
        update_h(c, i, rest);
    }

    if (!check_hashes())
        return "NIE";
    if (!check_tabs())
        return "NIE";
    if (!check_h(n))
        return "NIE";
    
    return "TAK";
}

string solve0()
{
    char c;
    int n = 0;
    while (cin >> c)
    {
        update_tabs(c);
        update_hashes(c);
        n++;
    }

    if (!check_hashes())
        return "NIE";
    if (!check_tabs())
        return "NIE";
    return "TAK";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    init();
    
    int n;
    cin >> n;
    if (n > 0)
    {
        if (n <= 500000)
            cout << solve_small(n);
        else
            cout << solven(n);
    }
    else
    {
        cout << solve0();
    }
    
    return 0;
}