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
/*
 *  Copyright (C) 2020  Paweł Widera
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details:
 *  http://www.gnu.org/licenses/gpl.html
 */
#include <iostream>
#include <array>
#include <string>
#include <vector>
using namespace std;

#define LETTERS 26

void preprocess(int n, array<int, LETTERS>& counter, array<vector<int>, LETTERS>& positions) {
	char letter;
	int toy;

	for (int i = 0; i < n; ++i) {
		cin >> letter;
		toy = int(letter) - 97;
		++counter[toy];
		positions[toy].push_back(i);
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	array<int, LETTERS> counter_start = {0};
	array<int, LETTERS> counter_end = {0};
	array<vector<int>, LETTERS> positions_start;
	array<vector<int>, LETTERS> positions_end;

	int n;
	cin >> n;

	preprocess(n, counter_start, positions_start);
	preprocess(n, counter_end, positions_end);

	// check if letter frequencies are the same
	for (int i = 0; i < LETTERS; ++i) {
		if (counter_start[i] != counter_end[i]) {
			cout << "NIE" << endl;
			return 0;
		}
	}

	// check if sum of paired distances is even for all letters
	for (int i = 0; i < LETTERS; ++i) {
		int sum = 0;
		for (unsigned int j = 0; j < positions_start[i].size(); ++j) {
			sum += abs(positions_start[i][j] - positions_end[i][j]);
		}
		if (sum % 2 == 1) {
			cout << "NIE" << endl;
			return 0;
		}
	}

	cout << "TAK" << endl;
	return 0;
}