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

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=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();
}
/* 考虑贪心,假设我们已经考虑到了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了。 */ 

原文地址:https://www.cnblogs.com/QingyuYYYYY/p/11829332.html

时间: 2024-07-31 05:35:38

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

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

链接:https://www.luogu.com.cn/problem/CF1243B2 题意:给你长度为n的两个字符串s和t,你可以最多进行2*n次操作,每次操作选择i和j,然后交换s[i]和t[j],问你能否使得两个字符串相同 构造方法:假如(0~i)部分s和t已经相等,在i位置时首先在(i+1~t.size()-1)里找有没有和t[i]相同的字符,如果找到,则交换s[i]和t[j],如果找不到,则在s中找,找到之后,先将s[j]与t[t.size()-1],交换,再将s[i]与t[t.si

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. You can hack this problem only if you solve both problems. After struggling and failing many times, Ujan decided to try to clean up his house again. He

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

链接: https://codeforces.com/contest/1234/problem/B2 题意: The only difference between easy and hard versions are constraints on n and k. You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most

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, a considerable part of which are his math notebooks: it is time to sort them out. This time he found an old dusty graph theory notebook with a descr

Codeforces Round #599 (Div. 2)

A - Maximum Square 题意:给 \(n\) 块宽度为 \(1\) 长度为 \(a_i\) 的木板,把这些木板拼在一起,求最大形成的正方形的边长. 题解:贪心,从大到小排序,然后找第一个满足 \(a_i<i\) 的位置break掉. #include<bits/stdc++.h> using namespace std; typedef long long ll; int n, a[1005]; int main() { #ifdef KisekiPurin freopen

Codeforces Round #575 (Div. 3) D1. RGB Substring (easy version)

Codeforces Round #575 (Div. 3) D1 - RGB Substring (easy version) The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given a

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

原题 https://codeforces.com/contest/1249/problem/B2 这道题一开始给的数组相当于地图的路标,我们只需对每个没走过的点进行dfs即可 #include <bits/stdc++.h> using namespace std;const int maxn=2e5+20;int a[maxn],b[maxn],c[maxn];int dfs(int pos,int step){//传递坐标与步数 if(b[pos]==1){//再次遇到b[pos],返回

Codeforces Round #599 (Div. 2)D 边很多的只有0和1的MST

题:https://codeforces.com/contest/1243/problem/D 分析:找全部可以用边权为0的点连起来的全部块 然后这些块之间相连肯定得通过边权为1的边进行连接 所以答案就是这些块的总数-1: #include<bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back const int M=1e5+5; set<int>s,g[M]; int vis[

Codeforces Round #599 (Div. 2) Tile Painting

题意:就是给你一个n,然后如果  n mod | i - j | == 0  并且 | i - j |>1 的话,那么i 和 j 就是同一种颜色,问你最大有多少种颜色? 思路: 比赛的时候,看到直接手推,发现有点东西,直接打表找出了规律 —— 如果 n的质因子只有一个,那么总数就是 那个 质因子.其它都为 1. 今天上课的时候无聊,还是试着推了一下原理. 1.如果一个数只有一个质因子 x ,那么  n-x .n-2x.n-3x ……等等全为一种颜色,也就是说每隔 x个就是同种颜色,这样的话就是有