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

using namespace std;

void merge(int start, int middle, int end, char tab[], char t[]){
    int i = start;
    int j = middle + 1;
    int k = i;
    for(int p = start; p <= end; p++){
        t[p] = tab[p];
    }
    while(i <= middle && j <= end){
        if(t[i] >= t[j]){
            tab[k] = t[i];
            i++;
        }else{
            tab[k] = t[j];
            j++;
        }
        k++;
    }
    while(i <= middle){
        tab[k] = t[i];
        i++;
        k++;
    }
}

void mergesort(int start, int end, char tab[], char t[]){
    int middle;
    if(start != end){
        middle = (start + end) / 2;
        mergesort(start, middle, tab, t);
        mergesort(middle + 1, end, tab, t);
        merge(start, middle, end, tab, t);
    }
}

int main()
{
    ios_base::sync_with_stdio(0); 
    cin.tie(0);
    int n;
    cin >> n;
    string zab1, zab2;
    cin >> zab1 >> zab2;
    char tab1[n];
    char tab2[n];
    for(int i = 0; i < n; i++){
        tab1[i] = zab1[i];
        tab2[i] = zab2[i];
    }
    char t1[n];
    char t2[n];
    mergesort(0, n-1, tab1, t1);
    mergesort(0, n-1, tab2, t2);
    int czyDziala = 0;
    for(int i = 0; i < n; i++){
        if(tab1[i] != tab2[i]){
            czyDziala = 1;
            break;
        }
    }
    int wynik = 0;
    if(czyDziala == 0){
        int i = 0;
        for(int i = 0; i < n; i++){
            int j = 0;
            while(j < n){
                if(zab1[i] == zab2[j] && (j - i)%2 == 0){
                    wynik = wynik + 1;
                    zab2[j] = '0';
                    break;
                }
                j++;
            }
        }
    }
    
    if(wynik == n) cout << "TAK";
    else cout << "NIE";
    return 0;
}