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
#include <iostream>
#include <string>
using namespace std;

void reverse(string string, int from, int to) {
	int indexFrom = from;
	int indexTo = to;
	char charCopy;
	while (indexFrom < indexTo) {
		charCopy = string[indexFrom];
		string[indexFrom] = string[indexTo];
		string[indexTo] = charCopy;
		indexFrom++;
		indexTo--;
	};
}

int main() {
	int count;
	string bitus;
	string bajtus;
	cin >> count;
	cin >> bitus;
	cin >> bajtus;

	bool end = true;
	int indexFrom ;
	int indexTo;
	bool reversed = true;

	while (reversed && end) {
		reversed = false;
		indexFrom = -1;
		indexTo = -1;

		for (int index = 0; index <= count; index++)
		{
			if (index < count
				&& indexFrom == -1
				&& bitus[index] != bajtus[index]) {
				indexFrom = index;
				continue;
			}

			if (index < count && (indexFrom == -1) && (bitus[index] == bajtus[index])) {
				indexTo = index - 1;
				continue;
			}

			if (index == count) {
				indexTo = count - 1;
				end = false;
			}

			if ((indexFrom != -1) && (indexTo != -1)
				&& ((indexTo - indexFrom + 1) % 2 == 1)
				&& (indexTo - indexFrom > 0)) 
			{
				reverse(bitus, indexFrom, indexTo);
				indexFrom = -1;
				indexTo = -1;

				reversed = true;
			}
		}
		// cout << bitus;
	}

	if (bitus.compare(bajtus) == 0)
		cout << "TAK";
	else
		cout << "NIE";

	return 0;
}