Character Swap (Hard Version)【字符串大模拟】

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.

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 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.

Example
inputCopy
4
5
souse
houhe
3
cat
dog
2
aa
az
3
abc
bca
outputCopy
Yes
1
1 4
No
No
Yes
3
1 2
3 1
2 3

package Main;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

public class Main {
	static int maxn = 1000010;
	static String s1,s2;
	static HashMap<Character,Integer> mp = new HashMap<Character,Integer>();
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int t = input.nextInt();
		while((t--)>0){
			mp.clear();
			int n = input.nextInt();
			s1 = input.next();
			s2 = input.next();
			StringBuffer str1 = new StringBuffer(s1);
			StringBuffer str2 = new StringBuffer(s2);
			for(int i=0;i<n;i++){
				mp.put(str1.charAt(i), !mp.containsKey(str1.charAt(i))?1:mp.get(str1.charAt(i))+1);
				mp.put(str2.charAt(i), !mp.containsKey(str2.charAt(i))?1:mp.get(str2.charAt(i))+1);
			}
			boolean flag=true;
			for(int i=0;i<n;i++){
				if(mp.get(str1.charAt(i))%2==0 && mp.get(str2.charAt(i))%2==0) continue;
				else{
					flag=false;
					break;
				}
			}
			if(!flag) System.out.println("No");
			else{
				System.out.println("Yes");
				System.out.println(2*n);
				for(int i=0;i<n;i++){
					int p=-1;
					for(int j=i;j<n;j++){
						if(str2.charAt(j)==str1.charAt(i)){
							p=j;
							break;
						}
					}
					char tmp;
					if(p==-1){
						for(int j=i+1;j<n;j++){
							if(str1.charAt(i)==str1.charAt(j)){
								p=j;
								break;
							}
						}
						System.out.println(p+1 + " " + (i+1));
						tmp=str1.charAt(p); str1.setCharAt(p,str2.charAt(i));
						str1.setCharAt(i, tmp);
						System.out.println(i+1 + " " + (i+1));
						continue; 
					}
					System.out.println(n + " " + (p+1));
					tmp=str1.charAt(n-1); str1.setCharAt(n-1,str2.charAt(p));
					str2.setCharAt(p, tmp);
					System.out.println(n + " " + (i+1));
					tmp=str1.charAt(n-1); str1.setCharAt(n-1,str2.charAt(i));
					str2.setCharAt(i, tmp);
				}
			}
		}
	}
}

发布了444 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zt2650693774/article/details/103027028
今日推荐