This problem is different from the easy version. In this version Ujan makes at most 2n2n swaps. In addition, k≤1000,n≤50k≤1000,n≤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 ss and tt of length nn 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 2n2n times: he takes two positions ii and jj (1≤i,j≤n1≤i,j≤n, the values ii and jj can be equal or different), and swaps the characters sisi and tjtj.

Ujan's goal is to make the strings ss and tt equal. He does not need to minimize the number of performed operations: any sequence of operations of length 2n2n or shorter is suitable.

Input

The first line contains a single integer kk (1≤k≤10001≤k≤1000), the number of test cases.

For each of the test cases, the first line contains a single integer nn (2≤n≤502≤n≤50), the length of the strings ss and tt.

Each of the next two lines contains the strings ss and tt, each having length exactly nn. 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 2n2n operations and "No" otherwise. You can print each letter in any case (upper or lower).

In the case of "Yes" print mm (1≤m≤2n1≤m≤2n) on the next line, where mm is the number of swap operations to make the strings equal. Then print mm lines, each line should contain two integers i,ji,j (1≤i,j≤n1≤i,j≤n) meaning that Ujan swaps sisi and tjtj during the corresponding operation. You do not need to minimize the number of operations. Any sequence of length not more than 2n2n is suitable.

Example
input

Copy
4
5
souse
houhe
3
cat
dog
2
aa
az
3
abc
bca
output

Copy
Yes
1
1 4
No
No
Yes
3
1 2
3 1
2 3
#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=; i<s.size(); i++) {
if(s[i]!=t[i]) {
int flag = ;
for(int j=i+; j<t.size(); j++) {
if(t[j]==t[i]) {
flag = ;
op.push_back(make_pair(i+,j+));
swap(s[i],t[j]);
break;
}
}
if(flag==) {
for(int j=i+; j<s.size(); j++) {
if(s[j]==t[i]) {
flag = ;
op.push_back(make_pair(j+,t.size()));
swap(s[j],t[t.size()-]);
op.push_back(make_pair(i+,t.size()));
swap(s[i],t[t.size()-]);
break;
}
}
}
if(flag==) {
puts("NO");
return;
}
}
}
puts("YES");
cout<<op.size()<<endl;
for(int i=; i<op.size(); i++) {
cout<<op[i].first<<" "<<op[i].second<<endl;
}
return;
}
int main() {
int t;
scanf("%d",&t);
while(t--)
solve();
}
/* 考虑贪心,假设我们已经考虑到了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次,就可以使得s==t了。 */

Codeforces Round #599 (Div. 2) B2. Character Swap (Hard Version)的更多相关文章

  1. Codeforces Round #599 (Div. 2) B2. Character Swap (Hard Version) 构造

    B2. Character Swap (Hard Version) This problem is different from the easy version. In this version U ...

  2. Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version) 水题

    B1. Character Swap (Easy Version) This problem is different from the hard version. In this version U ...

  3. Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version)

    This problem is different from the hard version. In this version Ujan makes exactly one exchange. Yo ...

  4. Codeforces Round #590 (Div. 3) B2. Social Network (hard version)

    链接: https://codeforces.com/contest/1234/problem/B2 题意: The only difference between easy and hard ver ...

  5. Codeforces Round #599 (Div. 2)

    久违的写篇博客吧 A. Maximum Square 题目链接:https://codeforces.com/contest/1243/problem/A 题意: 给定n个栅栏,对这n个栅栏进行任意排 ...

  6. Codeforces Round #599 (Div. 2) D. 0-1 MST(bfs+set)

    Codeforces Round #599 (Div. 2) D. 0-1 MST Description Ujan has a lot of useless stuff in his drawers ...

  7. Codeforces Round #599 (Div. 2)的简单题题解

    难题不会啊…… 我感觉写这个的原因就是因为……无聊要给大家翻译题面 A. Maximum Square 简单题意: 有$n$条长为$a_i$,宽为1的木板,现在你可以随便抽几个拼在一起,然后你要从这一 ...

  8. Codeforces Round #595 (Div. 3)B2 简单的dfs

    原题 https://codeforces.com/contest/1249/problem/B2 这道题一开始给的数组相当于地图的路标,我们只需对每个没走过的点进行dfs即可 #include &l ...

  9. B2. Character Swap (Hard Version)

    链接: http://codeforces.com/contest/1243/problem/B2 题目大意: 两个字符串,判断能否通过交换为从而使得这两个字符串完全一致,如不可以的话,直接输出NO, ...

随机推荐

  1. Linux的VMware虚拟机无法上网问题

    很多时候,Linux无法上网,不管改成nat模式还是主机模式都不行.这时候可以选择进行重置: 首先点击编辑,之后点击虚拟网络编辑器, 然后移除VMnet0和VMnet8网络,点击确定: 然后再打开虚拟 ...

  2. sqlalchemy_mptt一次调优

    问题背景: 我用sqlalchemy_mptt构建了一个多级分类项目,数据库用了sqlite.随着数据条数越来越多,写入速度逐渐变慢,一棵树的插入甚至需要1分钟,远远不能满足需求 分析思路: 1. 批 ...

  3. var s=+newDate();

    var s=+newDate(); 解释如下:=+是不存在的; +new Date()是一个东西; +相当于.valueOf(); 看到回复补充一下.getTime()这个也是得到毫秒数 //4个结果 ...

  4. 【Python】1.PyQT5界面初尝试

    1->通过pycharm打开QTdesigner.创建新Form. 2->选择Widget创建 3->不添加任何控件 点击保存 pycharm列表如下 后缀名位ui的  即QTdes ...

  5. LED Keychain-Ideal For Mass Promotions

    Looking for something memorable to remind people of your business or nonprofit? Consider custom LED ...

  6. 多项式乘法逆元 - NTT

    递归求解即可 #include <bits/stdc++.h> using namespace std; #define int long long namespace NTT { #de ...

  7. [SDOI2017] 新生舞会 - 二分图最大权匹配,分数规划,二分答案

    有一个二分图,每个部都有 \(n\) 个点,每条边有两个参数 \(a_e, b_e\),求一种匹配,使得 \(\sum a_i / \sum b_i\) 最大 Solution 显然的分数规划,考虑二 ...

  8. HDU-2036 改革春风吹满地 (数学)

    Problem Description "改革春风吹满地,不会AC没关系;实在不行回老家,还有一亩三分地.谢谢!(乐队奏乐)" 话说部分学生心态极好,每天就知道游戏,这次考试如此简 ...

  9. AcWing 1020. 潜水员 二维费用背包

    //体积最多是j 全部为0,v>=0 //体积恰好为j f[0][0]=0,f[i]=无穷,v>=0 //体积至少是j f[0][0]=0,f[i]=无穷,体积为负数时于0取大 #incl ...

  10. 牛客多校第七场H Pair 数位dp理解

    Pair 题意 给出A B C,问x取值[1,A]和y取值[1,B]存在多少组pair<x,y>满足以下最小一种条件,\(x \& y >c\),\(x\) xor \(y& ...