Codeforces Round #599 (Div. 2) B2

Character Swap (Hard Version)

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

This problem is different from the easy version. In this version Ujan makes at most 2n swaps. In addition, k≤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 s and tt of length n 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 2n times: he takes two positions i and j (1≤i,j≤n, the values i and j can be equal or different), and swaps the characters si and tj.

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 2n or shorter is suitable.

Input

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

For each of the test cases, the first line contains a single integer n (2≤n≤50), the length of the strings s and t.

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

In the case of "Yes" print mm (1≤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,j(1≤i,j≤n) meaning that Ujan swaps si and tj during the corresponding operation. You do not need to minimize the number of operations. Any sequence of length not more than 2n 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

 

首先考虑无解的情况。
若一个字母在两个串中的出现次数为奇数,一定无解。
记 i 表示当前枚举到第 i 位。再枚举一个 j 起中转作用。
分三种情况讨论。

  • si=ti,满足条件,枚举下一位即可。
  • si​=sj​,将 sj​ 与ti​ 交换即可。
  • si​=tj​,先将 sj​ 与jtj​ 交换,再将 sj​ 与ti​ 交换即可。

 

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize(2)
#include <iostream>
#include <iomanip>
#include <bitset>
#include <vector>
#include <string>
#include <math.h>
#include <cstring>
#include <stdio.h>
#include <cstring>
#include <stdlib.h>
#include <cstdlib>
#include <ctime>
#include <climits>
#include <cstdio>
#include <set>
#include <ctype.h>
#include <deque>
#include <string>
#include <map>
#include <utility>
#include <queue>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <algorithm>
#include <iomanip>
#include <string.h>
#include <stack>
#include <assert.h>
#define fi first
#define se second
#define pb(a)  push_back(a)
#define Q 1000000
#define EXP  1.000000000000000
#define EPS 1E-12
#define m_p make_pair
#define ft first
#define sc second
#define int long long
#define _rep(i,a,b) for(int i=a;i<=b;i++)
#define _ref(i,a,b) for(int i=a;i>=b;i--)
#define _res(i,a,b) for(int i=a;i<=b;i+=2)
const int N=1010;
const int mod=998244353;
string s,t;
vector<pair<int,int>>ans;
int qsm(int x,int n)
{
    int res=1;
    while(n)
    {
        if(n&1)res=res*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return res;
}
int cnt[26],n;
void solve()
{
    cin>>n;
    cin>>s>>t;

    s=" "+s;
    t=" "+t;

    for(int i=0;i<26;i++)
    {
        cnt[i]=0;
    }

    for(int i=1;i<=n;i++)
    {
        cnt[s[i]-'a']++;
        cnt[t[i]-'a']++;
    }

    for(int i=0;i<26;i++)
    {
        if(cnt[i]&1)
        {
            cout<<"No"<<"\n";
            return;
        }
    }

    ans.clear();

    for(int i=1;i<=n;i++)
    {
        if(s[i]==t[i])continue;
        for(int j=i+1;j<=n;j++)
        {
            if(s[i]==s[j])
            {
                ans.push_back({j,i});
                swap(s[j],t[i]);
                break;
            }
            if(s[i]==t[j])
            {
                ans.push_back({j,j});
                swap(s[j],t[j]);
                ans.push_back({j,i});
                swap(s[j],t[i]);
                break;
            }
        }
    }
    cout<<"Yes"<<"\n";
    cout<<ans.size()<<"\n";
    for(auto x:ans)
    {
        cout<<x.first<<" "<<x.second<<"\n";
    }
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        solve();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值