A13065 | Character Swap (Hard Version)
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
This problem is different from the easy version. In this version Ujan makes at most $2n$ swaps. In addition, $k \le 1000, n \le 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 \le i,j \le n$ , the values $i$ and $j$ can be equal or different), and swaps the characters $s_i$ and $t_j$ .
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.
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 \le i,j \le n$ , the values $i$ and $j$ can be equal or different), and swaps the characters $s_i$ and $t_j$ .
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 \leq k \leq 1000$ ), the number of test cases.
For each of the test cases, the first line contains a single integer $n$ ( $2 \leq n \leq 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 of the test cases, the first line contains a single integer $n$ ( $2 \leq n \leq 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 \le m \le 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 \le i, j \le n$ ) meaning that Ujan swaps $s_i$ and $t_j$ during the corresponding operation. You do not need to minimize the number of operations. Any sequence of length not more than $2n$ is suitable.
In the case of "Yes" print $m$ ( $1 \le m \le 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 \le i, j \le n$ ) meaning that Ujan swaps $s_i$ and $t_j$ during the corresponding operation. You do not need to minimize the number of operations. Any sequence of length not more than $2n$ is suitable.
输入输出样例
输入 #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
In the first test case, Ujan can swap characters $s_1$ and $t_4$ , obtaining the word "house".
In the second test case, it is not possible to make the strings equal using exactly one swap of $s_i$ and $t_j$ .
In the second test case, it is not possible to make the strings equal using exactly one swap of $s_i$ and $t_j$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted