lina_os

Untitled

Apr 26th, 2025
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define ull unsigned long long
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  9.     ll tt;
  10.     cin >> tt;
  11.     while (tt--) {
  12.         ll n;
  13.         cin >>n;
  14.         string s,t;
  15.         cin >> s >> t;
  16.         vector<ll>v(n);
  17.         for (auto &i:v) cin >> i;
  18.         map<char,vector<pair<ll,ll>>>m;
  19.         for (int i=0; i<n; i++) {
  20.             m[s[i]].push_back({v[i],i});
  21.         }
  22.         for (int i=0; i<n; i++) {
  23.             int f=1;
  24.             if (s[i]!=t[i]) {
  25.                 f=0;
  26.                 for (auto &x:m[t[i]]) {
  27.                     if (x.first==v[i]) {
  28.                         f=1;
  29.                         swap(s[i],s[x.second]);
  30.                         for (auto &y:m[s[i]]) {
  31.                             if (y.second==i) {
  32.                                 swap(y.second,x.second);
  33.                                 break;
  34.                             }
  35.                         }
  36.                     }
  37.                     if (f) break;
  38.                 }
  39.             }
  40.             if (!f) break;
  41.         }
  42.         if (s==t) cout << "YES" << endl;
  43.         else cout << "NO" << endl;
  44.     }
  45.     return 0;
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment