Link: https://www.luogu.com.cn/problem/CF1243B2
Topic: To give you two strings s and t of length n, you can perform up to 2 * n operations, select i and j for each operation, and then exchange s [i] and t [j], ask if you can Whether to make the two strings the same
Construction method: If the (0 ~ i) parts s and t are already equal, at the i position, first find whether there is the same character as t [i] in (i + 1 ~ t.size ()-1), if found , Then exchange s [i] and t [j], if not found, then find in s, after finding, first exchange s [j] and t [t.size ()-1], then exchange s [i] and t [t.size ()-1] exchange this number of times at most 2 * n times; then s = t;
Code:
#include<bits/stdc++.h> using namespace std; const int maxn=3e5+10; int n; string s,t; vector<pair<int ,int> > v; void solve() { cin>>n>>s>>t; v.clear(); for(int i=0; i<s.size(); i++) { if(s[i]!=t[i]) { int flag=0; for(int j=i+1; j<t.size(); j++) { if(t[i]==t[j]) { flag=1; v.push_back(make_pair(i+1,j+1)); swap(s[i],t[j]); break; } } if(!flag) { for(int j=i+1; j<s.size(); j++) { if(t[i]==s[j]) { flag=1; v.push_back(make_pair(j+1,t.size())); swap(s[j],t[t.size()-1]); v.push_back(make_pair(i+1,t.size())); swap(s[i],t[t.size()-1]); break; } } } if(!flag) { puts("NO\n"); return ; } } } puts("YES\n"); cout<<v.size()<<endl; for(int i=0; i<v.size(); i++) cout<<v[i].first<<" "<<v[i].second<<endl; return ; } int main() { int t; cin>>t; while(t--) solve(); return 0;
}