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
#include <iostream>
#include <string>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    
    int n;
    string s1, s2;
    int count1[26] = {0};
    int count2[26] = {0};

    cin >> n;
    cin >> s1 >> s2;

    for (int i = 0; i < n; ++i) {
        ++count1[s1[i] - 'a'];
        ++count2[s2[i] - 'a'];
    }

    for (int i = 0; i < 26; ++i) {
        if (count1[i] != count2[i]) {
            cout << "NIE";
            return 0;
        }
    }

    if (n < 3) {
        for (int i = 0; i < n; ++i) {
            if (s1[i] != s2[i]) {
                cout << "NIE";
                return 0;
            }
        }
    }

    char c;
    int j;
    bool letterFound;
    bool usedPoz[n] = {0};

    for (int i = 0; i < n; ++i) {
        c = s1[i];
        letterFound = false;
        // cout << "searching " << c << "\n";
        j = i;
        // cout << "searching forward \n"; 
        while (!letterFound && j < n) {
            if (!usedPoz[j] && c == s2[j]) {
                // cout << "found on " << j << "\n";
                letterFound = true;
                usedPoz[j] = true;
            }
            j += 2;
        }
        if (!letterFound) {
            j = i - 2;
            // cout << "searching backward \n";
        }
        while (!letterFound && j >= 0) {
            if (!usedPoz[j] && c == s2[j]) {
                // cout << "found on " << j << "\n";
                letterFound = true;
                usedPoz[j] = true;
            }
            j -= 2;
        }
        if (!letterFound) {
            cout << "NIE";
            return 0;
        }
    }

    cout << "TAK";
    return 0;
}