Time Limit | 1000 ms |
---|---|
Memory limit | 262144 kB |
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 t 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 s and t equal. He does not need to minimize the number of performed operations: any sequence of operations of length 2n or shorter is suitable.
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.
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 m (1≤m≤2n) on the next line, where m is the number of swap operations to make the strings equal. Then print m 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.
4
5
souse
houhe
3
cat
dog
2
aa
az
3
abc
bca
Yes
1
1 4
No
No
Yes
3
1 2
3 1
2 3
两个长度为 n n n的字符串 s s s和 t t t,可以将 s s s的任意位置的字符和 t t t的任意位置的字符进行位置交换,问在交换次数不超过 2 ∗ n 2*n 2∗n的情况下是否可以使两个字符串变成相等的
并且输出位置交换的 s s s和 t t t的位置下标
可以先排除一定不可能的情况,就是字符的个数为奇数的时候是不可能相等的
其次考虑两种交换的情况
第一种:前提( s t r 1 [ i ] = = s t r 1 [ j ] & & s t r 1 [ j ] ! = s t r 2 [ j ] str1[i] == str1[j] \&\& str1[j] != str2[j] str1[i]==str1[j]&&str1[j]!=str2[j])
a b a c c b aba\\ccb abaccb
a 2 a_2 a2直接与 c 1 c_1 c1进行交换(下标表示第几个同样的字母)
第二种:前提( s t r 1 [ i ] = = s t r 2 [ j ] & & s t r 1 [ j ] ! = s t r 2 [ j ] str1[i] == str2[j] \&\& str1[j] != str2[j] str1[i]==str2[j]&&str1[j]!=str2[j])
a b c b c a abc\\bca abcbca
c 1 → a 2 , a 2 → c 1 c_1 \rightarrow a_2, a_2 \rightarrow c_1 c1→a2,a2→c1(下标表示原本第几行的字母)
a 2 → b 2 , b 2 → a 2 a_2 \rightarrow b_2, b_2 \rightarrow a_2 a2→b2,b2→a2
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define endc std::ios::sync_with_stdio(false); // 关掉c++流
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout)
#define mes(a, b) memset(a, b, sizeof a)
#define qwq(i, j) for(int i = 0; i < j; ++i)
#define qeq(i, j) for(int i = 1; i <= j; ++i)
#define isdigit(a) ((a)>='0'&&(a)<='9')
#define xiao(a) ((a)>='a'&&(a)<='z')
#define da(a) ((a)>='A'&&(a)<='Z')
#define pii pair
#define lowbit(x) x & (-x)
#define fi first
#define se second
#define lson id<<1
#define rson id<<1|1
typedef unsigned long long int ull;
typedef long long int ll;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const ll INF = 1e18 + 100;
const int maxm = 1e6 + 6;
const int maxn = 100 + 10;
const int mod = 1e9+7;
using namespace std;
int n, m, cas, tol = 0;
int head[maxn];
struct Edge {
int u, v, w;}edge[maxm];
template<typename T>void re(T &x){
x = 0; int f = 0; char ch = getchar();while(!isdigit(ch)){
if(ch == '-') f=1; ch=getchar();}while(isdigit(ch)){
x=(x<<3)+(x<<1)+ch-'0'; ch=getchar();}x = f?-x:x;}
// templateT fpow(T a, ll b) {T ans = 1; while(b) {if(b & 1) ans = a * ans % mod; a = a * a % mod; b >>= 1;}return ans;}
// inline void adde(int u, int v, int w) {edge[tol] = Edge{v, head[u], w}; head[u] = tol++;}
//POJ 3463 - Sightseeing
char str1[55], str2[55];
int cnt[30], res[105];
int main() {
int T;
scanf("%d", &T);
while(T--) {
int n, k = 0;
scanf("%d%s%s", &n, str1, str2);
for(int i = 0; i < 30; ++i) cnt[i] = 0;
for(int i = 0; i < n; ++i) {
cnt[str1[i]-'a']++, cnt[str2[i]-'a']++;
}
int flag = 0;
for(int i = 0; i < 30; ++i) {
if(cnt[i] % 2) {
flag = 1;
break;
}
}
if(flag) {
puts("No");
continue;
}
int k2 = 0;
for(int i = 0; i < n; ++i) {
if(str1[i] == str2[i]) continue;
for(int j = i + 1; j < n; ++j) {
if(str1[i] == str1[j] && str1[j] != str2[j]) {
//aba
//ccb a2 <-> c1 的情况, 交换1次
res[k2++] = j, res[k2++] = i;
swap(str1[j], str2[i]);
break;
}else if(str1[i] == str2[j] && str1[j] != str2[j]) {
//abc
//bca c <-> a', a' <-> b 的情况, 交换2次
res[k2++] = j, res[k2++] = j, res[k2++] = j, res[k2++] = i;
swap(str1[j], str2[j]);
swap(str1[j], str2[i]);
break;
}
}
}
if(k2/2 > 2*n) {
// 记得要加个判断
puts("No");
continue;
}
printf("Yes\n%d\n", k2/2);
for(int i = 0; i < k2; i += 2) {
printf("%d %d\n", res[i] + 1, res[i+1] + 1);
}
}
return 0;
}