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
#include <bits/stdc++.h>
using namespace std;

string rotate(string input, int start, int end)
{
    string rotated_fragment = "";
    for (int i = end; i >= start; i--) rotated_fragment += input[i];

    string output = input;
    for (int i = start; i <= end; i++) output[i] = rotated_fragment[i-start];

    return output;
}

map<string, bool> already_checked;

bool recursive(string start, string target)
{
    
    if(start == target) return true;

    already_checked[start] = true;

    for (int length = 3; length <= start.size(); length+=2)
    {
        for (int left = 0; left + length - 1 < start.size(); left++)
        {
            int right = left + length - 1;
            string rotated = rotate(start, left, right);

            if(already_checked[rotated]) break;

            // /** DEBUG **/ printf("%i (%i, %i) => %s\n", length, left, right, rotated.c_str());
            // getchar();

            if(rotated == target) return true;
            else return recursive(rotated, target);
        }
    }

    // not found
    return false;
}

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

    int chars; string target, start;
    cin >> chars >> target >> start;
    
    bool result = recursive(start, target);
    if(result) cout << "TAK\n"; 
    else cout << "NIE\n";
}