#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int num; cin >> num; vector<char> in(num); vector<char> out(num); vector<pair<int, int>> inCounters(26, make_pair(0, 0)); vector<pair<int, int>> outCounters(26, make_pair(0, 0)); vector<vector<int>> indices(26); vector<int> indicesB(26, 0); char c; for (int i = 0; i < num; i++) { cin >> c; in[i] = c; if (i % 2 == 0) inCounters[c - 'a'].first++; else inCounters[c - 'a'].second++; indices[c - 'a'].push_back(i); } for (int i = 0; i < num; i++) { cin >> c; out[i] = c; if (i % 2 == 0) outCounters[c - 'a'].first++; else outCounters[c - 'a'].second++; } bool good = true; for (int i = 0; i < 26; i++) { if (inCounters[i].first != outCounters[i].first || inCounters[i].second != outCounters[i].second) { good = false; break; } } if (!good) { cout << "NIE" << endl; return 0; } for (int i = 0; i < num; i++) { const int inId = in[i] - 'a'; const int outId = out[i] - 'a'; if (in[i] != out[i]) { auto &inIndices = indices[inId]; auto &outIndices = indices[outId]; auto j = outIndices[indicesB[outId]]; iter_swap(in.begin() + i, in.begin() + j); indicesB[inId]++; indicesB[outId]++; inIndices.insert(lower_bound(inIndices.begin(), inIndices.end(), j), j); } else { indicesB[inId]++; } } cout << "TAK" << endl; return 0; }
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int num; cin >> num; vector<char> in(num); vector<char> out(num); vector<pair<int, int>> inCounters(26, make_pair(0, 0)); vector<pair<int, int>> outCounters(26, make_pair(0, 0)); vector<vector<int>> indices(26); vector<int> indicesB(26, 0); char c; for (int i = 0; i < num; i++) { cin >> c; in[i] = c; if (i % 2 == 0) inCounters[c - 'a'].first++; else inCounters[c - 'a'].second++; indices[c - 'a'].push_back(i); } for (int i = 0; i < num; i++) { cin >> c; out[i] = c; if (i % 2 == 0) outCounters[c - 'a'].first++; else outCounters[c - 'a'].second++; } bool good = true; for (int i = 0; i < 26; i++) { if (inCounters[i].first != outCounters[i].first || inCounters[i].second != outCounters[i].second) { good = false; break; } } if (!good) { cout << "NIE" << endl; return 0; } for (int i = 0; i < num; i++) { const int inId = in[i] - 'a'; const int outId = out[i] - 'a'; if (in[i] != out[i]) { auto &inIndices = indices[inId]; auto &outIndices = indices[outId]; auto j = outIndices[indicesB[outId]]; iter_swap(in.begin() + i, in.begin() + j); indicesB[inId]++; indicesB[outId]++; inIndices.insert(lower_bound(inIndices.begin(), inIndices.end(), j), j); } else { indicesB[inId]++; } } cout << "TAK" << endl; return 0; } |