B2. Character Swap (Hard Version)
This problem is different from the easy version. In this version Ujan makes at most 2𝑛 swaps. In addition, 𝑘≤1000,𝑛≤50 and it is necessary to print swaps themselves. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.
After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.
Ujan has two distinct strings 𝑠 and 𝑡 of length 𝑛 consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation at most 2𝑛 times: he takes two positions 𝑖 and 𝑗 (1≤𝑖,𝑗≤𝑛, the values 𝑖 and 𝑗 can be equal or different), and swaps the characters 𝑠𝑖 and 𝑡𝑗.
Ujan's goal is to make the strings 𝑠 and 𝑡 equal. He does not need to minimize the number of performed operations: any sequence of operations of length 2𝑛 or shorter is suitable.
Input
The first line contains a single integer 𝑘 (1≤𝑘≤1000), the number of test cases.
For each of the test cases, the first line contains a single integer 𝑛 (2≤𝑛≤50), the length of the strings 𝑠 and 𝑡.
Each of the next two lines contains the strings 𝑠 and 𝑡, each having length exactly 𝑛. The strings consist only of lowercase English letters. It is guaranteed that strings are different.
Output
For each test case, output "Yes" if Ujan can make the two strings equal with at most 2𝑛 operations and "No" otherwise. You can print each letter in any case (upper or lower).
In the case of "Yes" print 𝑚 (1≤𝑚≤2𝑛) on the next line, where 𝑚 is the number of swap operations to make the strings equal. Then print 𝑚 lines, each line should contain two integers 𝑖,𝑗 (1≤𝑖,𝑗≤𝑛) meaning that Ujan swaps 𝑠𝑖 and 𝑡𝑗 during the corresponding operation. You do not need to minimize the number of operations. Any sequence of length not more than 2𝑛 is suitable.
Example
input
4
5
souse
houhe
3
cat
dog
2
aa
az
3
abc
bca
output
Yes
1
1 4
No
No
Yes
3
1 2
3 1
2 3
題意
給你兩個長度為n的字符串s和t,你可以進行最多2n次操作。
每次操作是選擇i和j,然后交換s[i]和t[j],問你能否使得兩個字符串相同。
輸出方案
題解
考慮貪心,假設我們已經考慮到了i位置,[0,i)區間的都已經相同了。
如果s[i]!=t[i]的情況,我們考慮首先交換s[i]和t[j],即能否在t里面找到和t[i]相同的;如果沒有,我們再從s里面去找即可。假設s[j]t[i],那么我們交換s[j]和t[t.size()-1],再交換s[i]和t[t.size()-1],只需要兩次操作就可以使得s[i]變成s[j]了,那么我們這樣最多操作2n次,就可以使得st了。
代碼
#include<bits/stdc++.h>
using namespace std;
int n;
string s,t;
vector<pair<int,int> >op;
void solve(){
op.clear();
cin>>n;
cin>>s>>t;
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[j]==t[i]){
flag = 1;
op.push_back(make_pair(i+1,j+1));
swap(s[i],t[j]);
break;
}
}
if(flag==0){
for(int j=i+1;j<s.size();j++){
if(s[j]==t[i]){
flag = 1;
op.push_back(make_pair(j+1,t.size()));
swap(s[j],t[t.size()-1]);
op.push_back(make_pair(i+1,t.size()));
swap(s[i],t[t.size()-1]);
break;
}
}
}
if(flag==0){
puts("NO");
return;
}
}
}
puts("YES");
cout<<op.size()<<endl;
for(int i=0;i<op.size();i++){
cout<<op[i].first<<" "<<op[i].second<<endl;
}
return;
}
int main(){
int t;
scanf("%d",&t);
while(t--)
solve();
}