CF1243B2.Character Swap (Hard Version)
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
本题与简单版本不同。在本版本中,Ujan 最多执行 2n 次交换操作。此外,k≤1000,n≤50,且必须输出具体的交换操作序列。若你成功解决本题,则可对本题进行 Hack;但若仅想 Hack 上一题(即简单版本),则必须同时解决本题与上一题。
在多次尝试并屡次失败后,Ujan 决定再次整理自己的房子。他决定先将字符串理顺。
Ujan 拥有两个长度均为 n 的互不相同的字符串 s 和 t,它们均由小写英文字母组成。他希望使这两个字符串相等。由于 Ujan 很懒,他最多执行 2n 次如下操作:选取两个位置 i 和 j(其中 1≤i,j≤n,i 与 j 可以相等或不同),然后交换字符 si 与 tj。
Ujan 的目标是使字符串 s 与 t 相等。他无需最小化操作次数:任何长度不超过 2n 的操作序列均符合要求。
输入格式
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.
第一行包含一个整数 k(1≤k≤1000),表示测试用例的数量。
对于每个测试用例,第一行包含一个整数 n(2≤n≤50),表示字符串 s 和 t 的长度。
接下来的两行分别包含字符串 s 和 t,每个字符串的长度恰好为 n。字符串仅由小写英文字母组成。保证两个字符串互不相同。
输出格式
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.
对于每个测试用例,如果 Ujan 能够在最多 2n 次操作内使两个字符串相等,则输出“Yes”;否则输出“No”。字母大小写均可(即可以全部大写或全部小写)。
若输出“Yes”,则在下一行输出 m(1≤m≤2n),其中 m 是使两字符串相等所需的交换操作次数。随后输出 m 行,每行包含两个整数 i,j(1≤i,j≤n),表示在该次操作中 Ujan 交换 si 和 tj。你无需最小化操作次数;只要操作序列长度不超过 2n 即可。
输入输出样例
输入#1
4 5 souse houhe 3 cat dog 2 aa az 3 abc bca
输出#1
Yes 1 1 4 No No Yes 3 1 2 3 1 2 3
输入解题思路,AI测评打分。不知道怎么写?