Codeforces Round #599 (Div. 2) B2. Character Swap (Hard Version)
This problem is different from the easy version. In this version Ujan makes at most 2n2n swaps. In addition, k≤1000,n≤50k≤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 ss and tt of length nn 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 2n2n times: he takes two positions ii and jj (1≤i,j≤n1≤i,j≤n, the values ii and jj can be equal or different), and swaps the characters sisi and tjtj.
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 2n2n or shorter is suitable.
The first line contains a single integer kk (1≤k≤10001≤k≤1000), the number of test cases.
For each of the test cases, the first line contains a single integer nn (2≤n≤502≤n≤50), the length of the strings ss and tt.
Each of the next two lines contains the strings ss and tt, each having length exactly nn. 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 2n2n operations and "No" otherwise. You can print each letter in any case (upper or lower).
In the case of "Yes" print mm (1≤m≤2n1≤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,ji,j (1≤i,j≤n1≤i,j≤n) meaning that Ujan swaps sisi and tjtj during the corresponding operation. You do not need to minimize the number of operations. Any sequence of length not more than 2n2n 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
#include<bits/stdc++.h>
using namespace std;
int n;
string s,t;
vector<pair<int,int> >op;
void solve() {
op.clear();
cin>>n;
cin>>s>>t;
for(int i=; i<s.size(); i++) {
if(s[i]!=t[i]) {
int flag = ;
for(int j=i+; j<t.size(); j++) {
if(t[j]==t[i]) {
flag = ;
op.push_back(make_pair(i+,j+));
swap(s[i],t[j]);
break;
}
}
if(flag==) {
for(int j=i+; j<s.size(); j++) {
if(s[j]==t[i]) {
flag = ;
op.push_back(make_pair(j+,t.size()));
swap(s[j],t[t.size()-]);
op.push_back(make_pair(i+,t.size()));
swap(s[i],t[t.size()-]);
break;
}
}
}
if(flag==) {
puts("NO");
return;
}
}
}
puts("YES");
cout<<op.size()<<endl;
for(int i=; i<op.size(); i++) {
cout<<op[i].first<<" "<<op[i].second<<endl;
}
return;
}
int main() {
int t;
scanf("%d",&t);
while(t--)
solve();
}
/* 考虑贪心,假设我们已经考虑到了i位置,[0,i)区间的都已经相同了。
如果s[i]!=t[i]的情况,我们考虑首先交换s[i]和t[j],即能否在t里面找到和t[i]相同的;
如果没有,我们再从s里面去找即可。
假设s[j]==t[i],那么我们交换s[j]和t[t.size()-1],再交换s[i]和t[t.size()-1],
只需要两次操作就可以使得s[i]变成s[j]了,那么我们这样最多操作2n次,就可以使得s==t了。 */
Codeforces Round #599 (Div. 2) B2. Character Swap (Hard Version)的更多相关文章
- Codeforces Round #599 (Div. 2) B2. Character Swap (Hard Version) 构造
B2. Character Swap (Hard Version) This problem is different from the easy version. In this version U ...
- Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version) 水题
B1. Character Swap (Easy Version) This problem is different from the hard version. In this version U ...
- Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version)
This problem is different from the hard version. In this version Ujan makes exactly one exchange. Yo ...
- Codeforces Round #590 (Div. 3) B2. Social Network (hard version)
链接: https://codeforces.com/contest/1234/problem/B2 题意: The only difference between easy and hard ver ...
- Codeforces Round #599 (Div. 2)
久违的写篇博客吧 A. Maximum Square 题目链接:https://codeforces.com/contest/1243/problem/A 题意: 给定n个栅栏,对这n个栅栏进行任意排 ...
- Codeforces Round #599 (Div. 2) D. 0-1 MST(bfs+set)
Codeforces Round #599 (Div. 2) D. 0-1 MST Description Ujan has a lot of useless stuff in his drawers ...
- Codeforces Round #599 (Div. 2)的简单题题解
难题不会啊…… 我感觉写这个的原因就是因为……无聊要给大家翻译题面 A. Maximum Square 简单题意: 有$n$条长为$a_i$,宽为1的木板,现在你可以随便抽几个拼在一起,然后你要从这一 ...
- Codeforces Round #595 (Div. 3)B2 简单的dfs
原题 https://codeforces.com/contest/1249/problem/B2 这道题一开始给的数组相当于地图的路标,我们只需对每个没走过的点进行dfs即可 #include &l ...
- B2. Character Swap (Hard Version)
链接: http://codeforces.com/contest/1243/problem/B2 题目大意: 两个字符串,判断能否通过交换为从而使得这两个字符串完全一致,如不可以的话,直接输出NO, ...
随机推荐
- Luogu1287 | 盒子与球 (排列组合)
贴一个和其他题解不一样的做法 QWQ 题意:让我们求出 N 个球放入 R 个盒子且每个盒子都必须放球方案数. 首先,对于每一个球,可以将其放入的盒子数量共有 R 个,所以我们可以知道如果无需满足每个盒 ...
- 原生js实现拖拽功能
1. 给个div,给定一些样式 <div class="drag" style="left:0;top:0;width:100px;height:100px&quo ...
- 安装SQL Server2008出现Restart computer failed的解决办法
1.打开注册表编辑器 2.找到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager双击文件夹 3.找到PendingF ...
- vue制作滚动条幅-跑马灯效果实例代码
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- eclipse配置tomcat,并部署一个Java web项目到tomcat上
引用链接:https://blog.csdn.net/cincoutcin/article/details/79408484 eclipse配置tomcat 1.windows——preference ...
- nginx 启动报错找不到nginx.pid文件
这个问题的出现应该是系统找不到nginx的配置文件nginx.conf,所以,我们要告诉系统配置文件的位置:' --- 使用nginx -c /usr/local/nginx/conf/nginx.c ...
- solr es调优化和问题排查
(1)TOP 显示当前进程状态,结合 ps -aux 可以看是哪一个服务.mpstat 可以看是cpu的负载 (2)TOP -H -u 用户名 显示该用户下 所有的线程. 还有pstree (3)js ...
- vue源码的入口(四)
我们之前提到过 Vue.js 构建过程,在 web 应用下,我们来分析 Runtime + Compiler 构建出来的 Vue.js,它的入口是 src/platforms/web/entry-ru ...
- pick the stone game
我该如何去触摸这类问题嘞! 取石子游戏 1堆石子有n个,两人轮流取. 先取者第1次可以取任意多个,但不能全部取完. 以后每次取的石子数不能超过上次取子数的2倍. 取完者胜.先取者负输出"Se ...
- 关于“教室派”APP的使用报告和相关建议
教室派APP能够很好的解决学生查询各教室占用情况这一问题,使用起来非常方便.用户可根据需要选取星期来查询不同教学楼教室使用情况. 编辑课表是其附带功能,但通过使用发现手动编辑课表效率太低,建议开发者加 ...