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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>

typedef long long int llint;

#define MAX_TAB (2<<20)

int n;
char tab[MAX_TAB];

bool solve_known_small_n()
{
	for(int i=0;i<n/2;++i)
		if(tab[i]!=tab[n-i-1])
			return false;
	return true;
}

bool read_and_solve_known_small_n()
{
	for(int i=0;i<n;++i)
		tab[i]=getchar();
	return solve_known_small_n();
}

struct hash_t
{
	static constexpr int num_primes=5;
	const int primes[num_primes]
	{
		999983,
		999979,
		999961,
		999959,
		999953
	};
	static constexpr int rolling_a_mult = 999599;

	static constexpr int num_values=6;

	llint sum;
	llint mult_mod[num_primes];

	llint rolling_mod[num_primes];
	llint rolling_mod_a[num_primes];

	hash_t()
	{
		sum=0;
		for(int i=0;i<num_primes;++i)
			mult_mod[i]=1;
		for (int i=0;i<num_primes;++i)
		{
			rolling_mod_a[i]=1;
			rolling_mod[i]=0;
		}
	}

	void calc(int len)
	{
		for (int i=0;i<len;++i)
			update(getchar());
	}

	void calc_tab(int start,int len)
	{
		for (int i=0;i<len;++i)
			update(tab[(start+i)%MAX_TAB]);
	}

	void calc_rev(int len)
	{
		for (int i=0;i<len;++i)
			update_rev(getchar());
	}

	void calc_tab_rev(int start,int len)
	{
		for (int i=0;i<len;++i)
			update_rev(tab[(start+i)%MAX_TAB]);
	}

	bool operator==(const hash_t& other) const
	{
		if(sum!=other.sum)
			return false;
		for(int i=0;i<num_primes;++i)
		{
			if(mult_mod[i]!=other.mult_mod[i])
				return false;
			if(rolling_mod[i]!=other.rolling_mod[i])
				return false;
		}
		return true;
	}

private:
	void update(char c)
	{
		update_symmetric(c);
		for(int i=0;i<num_primes;++i)
			update_rolling(i,c);
	}

	void update_rev(char c)
	{
		update_symmetric(c);
		for(int i=0;i<num_primes;++i)
			update_rev_rolling(i,c);
	}

	void update_symmetric(char c)
	{
		if (c)
		{
			sum += c;
			for(int i=0;i<num_primes;++i)
				mult_mod[i]=(mult_mod[i]*c)%primes[i];
		}
	}

	void update_rolling(int i,int c)
	{
		rolling_mod[i]=(rolling_mod[i]*rolling_a_mult+c)%primes[i];
	}

	void update_rev_rolling(int i,int c)
	{
		rolling_mod[i]=(rolling_mod[i]+c*rolling_mod_a[i])%primes[i];
		rolling_mod_a[i]=(rolling_mod_a[i]*rolling_a_mult)%primes[i];
	}
};

bool solve_known_large_n()
{
	hash_t front_hash;
	front_hash.calc(n/2);
	if(n&1)
		getchar();
	hash_t rev_back_hash;
	rev_back_hash.calc_rev(n/2);
	return front_hash==rev_back_hash;
}

bool solve_known_n()
{
	if(n<=MAX_TAB)
		return read_and_solve_known_small_n();
	return solve_known_large_n();
}

bool is_eos(char c)
{
	return !c||c=='\n';
}

bool continue_solve_unknown_large_n()
{
	hash_t front_hash;
	front_hash.calc_tab(0,MAX_TAB);

	hash_t rev_back_hash;
	int i=0;
	char c;
	while(i<MAX_TAB&&(c=getchar())&&c!='\n')
		tab[i++]=c;
	rev_back_hash.calc_tab_rev(i%MAX_TAB, MAX_TAB);
	if(is_eos(c))
	{
		if(!i)
		{
			n=MAX_TAB;
			return solve_known_small_n();
		}
		return front_hash==rev_back_hash;
	}

	while((c=getchar())&&c!='\n')
	{
		int next_index=(i++)%MAX_TAB;
		front_hash.calc_tab(next_index,1);
		tab[next_index]=c;
		rev_back_hash.calc_tab_rev(next_index, 1);
	}

	return front_hash==rev_back_hash;
}

bool solve_unknown_n()
{
	int i=0;
	char c;
	while(i<MAX_TAB&&(c=getchar())&&c!='\n')
		tab[i++]=c;
	if(is_eos(c))
	{
		n=i;
		return solve_known_small_n();
	}
	return continue_solve_unknown_large_n();
}

bool solve()
{
	return n?solve_known_n():solve_unknown_n();
}

int main()
{
	scanf("%d\n",&n);
	std::cout<<(solve()?"TAK":"NIE");
	return 0;
}