第二天叫醒我的不是闹钟,是梦想! 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

//首先两个串中每个字符出现的次数总和都是偶数次。
如果j>i 且 s[i]==s[j] 那么交换s[j]和t[i],这时候i这个位置上的字符就像等了。
如果 j>i 且 s[i]==t[j] 那么交换s[j]和t[j],再交换t[i]与s[j] ,那么i对应位置上的字符也相等了。
用结构体保存交换的坐标。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
char s[N],t[N];
struct node
{int x,y;
}a[N];
int num[26],tot;
int k,n;
int main()
{cin>>k;while(k--){memset(num,0,sizeof(num));tot=0;cin>>n>>(s+1)>>(t+1);for(int i=1;i<=n;i++){num[s[i]-'a']++;num[t[i]-'a']++;}bool f=false;for(int i=0;i<26;i++){if(num[i]&1){f=true;puts("No");break;}}if(f) continue;for(int i=1;i<=n;i++){if(s[i]==t[i]) continue;f=false;for(int j=i+1;j<=n;j++){if(s[j]==t[j]) continue;if(s[i]==s[j]){f=true;swap(t[i],s[j]);a[++tot]=node{j,i};break;}}if(f) continue;for(int j=i+1;j<=n;j++){if(s[j]==t[j]) continue;if(s[i]==t[j]){swap(s[j],t[j]);a[++tot]=node{j,j};swap(t[i],s[j]);a[++tot]=node{j,i};break;}}}puts("Yes");cout<<tot<<endl;for(int i=1;i<=tot;i++) cout<<a[i].x<<" "<<a[i].y<<endl;}return 0;
}

Character Swap (Hard Version)相关推荐

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

  2. Codeforces Round #599A~D题解

    Maximum Square 这是一道很简单的题目大意就是说给你一个几个木棍拼成一串问你从中扣出一个正方形的最大边长是多少 二分简单枚举一下可能的长度再判断一下就好了 #include <ios ...

  3. 去除警告: FutureWarning: In future, it will be treated as `np.float64 == np.dtype(float).type`.

    去除警告: FutureWarning: Conversion of the second argumecated. In future, it will be treated as `np.floa ...

  4. kafka安装_安装Kafka

    安装ZK ➜ Workspace tar -zxf zookeeper-3.4.6.tar.gz➜ Workspace mkdir -p data/zookeeper 配置文件: ➜ zookeepe ...

  5. 通过QMP/QGA与虚拟机进行交互

    1. QMP介绍 qmp全称是qemu monitor protocol,qemu对外提供了一个socket接口,称为qemu monitor,通过该接口,可以对虚拟机实例的进行管理. 2. QMP语 ...

  6. QR code 二维码基础入门教程

    QR code 二维码基础入门教程 本文为 QR Code Tutorial: Introduction 的总结,详细内容请查看原文 Introduction History and Informat ...

  7. Linux内核跟踪eBPF:bpftrace 参考指南

    bpftrace Reference Guide 推荐阅读:Linux内核跟踪eBPF:bpftrace一行教程 For a reference summary, see the README.md ...

  8. 可用的交换空间为 0 MB

    [oracle@oracle11g-std database]$ ./runInstaller -silent -ignorePrereq  -responseFile  /home/oracle/d ...

  9. 《用Castor 处理XML文档》学习笔记

    --Castor可以完成Java和XML的相互转换 前面有介绍过json-lib这个框架,在线博文: [url]http://www.cnblogs.com/hoojo/archive/2011/04 ...

最新文章

  1. Datawhale第四期组队学习团队成员
  2. 传感器的“脖子”卡在哪儿?
  3. 五. H.264的码流封装格式
  4. 马逊s3云存储接口_使用 Amazon S3 云服务轻松实现存储
  5. memcached命令行、Memcached数据导出和导入、php连接memcache、php的session存储到memcached...
  6. Hadoop(八)Java程序访问HDFS集群中数据块与查看文件系统
  7. windows 系统日志
  8. java ioutils 写入文件_IOUtils和FileUtils的学习笔记
  9. Latex 的基本语法
  10. 鼎捷易助8.0ERP系统制造业ERP系统介绍
  11. 经济基础知识(中级)【6】
  12. EfficientNet论文解读
  13. 写的一个棋盘格生成小程序,很简单,但是很实用,对于经常使用棋盘格的人来说很实用
  14. Win8系统鼠标右键突然失灵怎么办 win8桌面右键菜单失效怎么修复
  15. CrawlSpider爬取拉勾网,解决302问题。
  16. android框架服务 恶意软件,Android平台各类恶意软件及病毒概览
  17. 吉尼斯计算机打字记录,吉尼斯世界纪录打字
  18. 常间的黑客攻击手段以及预防方法整理
  19. 中科协发布2022“科创中国”开源创新榜 OceanBase开源社区入选
  20. Word中千分号怎么打?

热门文章

  1. LINUX IIO子系统分析之五IIO BUFFER子模块实现分析
  2. 那个卖了房子去大理的姑娘,4个月后又回来了
  3. XP Professional开机就要激活,否则无法登录桌面
  4. Windows 11 L2TP连接尝试失败,因为安全层在初始化与远程计算机的协商时遇到了一个处理错误
  5. 手动释放Linux显卡显存
  6. 华为Android10怎样root,华为Mate10 root教程_华为Mate10卡刷获取root权限的方法
  7. 邮箱地址是什么?什么是邮箱地址?电子邮箱地址大全
  8. 获取市场上的股票代码
  9. 英语单词记忆 词源法-思维导图(19)词源st/sist/stat/stin/stem-236
  10. python实现微信hook_GitHub - gemgin/wechathook: 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取...