B2. Character Swap (Hard Version)
链接:
http://codeforces.com/contest/1243/problem/B2
题目大意:
两个字符串,判断能否通过交换为从而使得这两个字符串完全一致,如不可以的话,直接输出NO,可以的话输出YES,并且输出每一步的交换位置。
思路:如果没个字符出现的次数为偶数次的话,那么一定可以成功,否则的话一定是NO。
如果说S[i]!=T[i],假如说,S中有与T[i]相同的元素,那么直接交换就可以了,操作次数为1,在T中找S[i]操作相同。
S中没有与T[i]相同的元素,我们保证了每个元素出先的次数为偶数次,那么在T中一定会有一个元素,我们只需要把在T中找一下S[i],然后将其与S中的某个元素交换一下,然后就变成了第一种情况,操作次数为2
所以如果可以的,我们最多操作2*n次。
#include<bits/stdc++.h>
using namespace std;
const int N=;
struct stu{
char a;
int x1;
}s1[N],t1[N];
int ar[N],br[N];
int arr[N];
void solve(){
memset(arr,,sizeof arr);
int n;
cin>>n;
string s,t;
cin>>s>>t;
for(int i=;i<n;i++){
arr[s[i]-'a']++;
arr[t[i]-'a']++;
}
for(int i=;i<;i++){
if(arr[i]&){
puts("NO");
return ;
}
}
int pos=;
printf("YES\n");
for(int i=;i<n;i++){
if(s[i]!=t[i]){
t1[pos].a=t[i];
t1[pos].x1=i;
s1[pos].a=s[i];
s1[pos++].x1=i;
}
}
int ans=;
int end1=s1[pos-].x1;
for(int i=;i<pos;i++){
bool flag=;
for(int j=i+;j<pos;j++){//从t中寻找与t相等的字符 ,找到后直接交换
if(t1[j].a==t1[i].a) {
ans++;
flag=;
ar[ans]=s1[i].x1+;
br[ans]=t1[j].x1+;
swap(s1[i].a,t1[j].a);
break;
}
}
if(flag) continue ;
for(int j=i+;j<pos;j++){//从s中寻找与s相等的字符
if(s1[j].a==s1[i].a){
flag=;
ans++;
ar[ans]=s1[j].x1+;
br[ans]=t1[i].x1+;
swap(s1[j].a,t1[i].a);
break;
}
}
if(flag) continue ;
for(int j=i+;j<pos;j++) //从t中寻找与s相等的,然后通过与某个数交换
{
if(s1[i].a==t1[j].a){
ans++;
ar[ans]=end1+;
br[ans]=t1[j].x1+;
ans++;
swap(t1[j].a,s1[pos-].a);
swap(s1[pos-].a,t1[i].a);
ar[ans]=end1+;
br[ans]=t1[i].x1+;
break;
}
}
}
printf("%d\n",ans);
for(int i=;i<=ans;i++){
cout<<ar[i]<<" "<<br[i]<<endl;
}
return ;
}
int main(){
int t;
cin>>t;
while(t--) solve();
return ;
}
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) B2. Character Swap (Hard Version)
This problem is different from the easy version. In this version Ujan makes at most 2n2n swaps. In a ...
- 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 ...
- 【CF1243B2】Character Swap (Hard Version)【思维】
题意:给定两个字符串,问是否存在交换方案使得两个字符串相同,方案为交换次数小于等于2n,且每次只交换s1与s2中的一个字符 题解:考虑从前往后枚举,当第i位不同时,考虑找后边的第j位,若存在这样的第j ...
- 【CF1243B1】Character Swap (Easy Version)【思维】
题意:给你两个字符串,问是否存在交换方案使得两个字符串变成一样的,方案为只交换一次且只交换s1与s2里的一个字符 题解:若一开始就相同,则存在交换方案 若一开始不同的位置为1个或大于2个,则不存在方案 ...
- CodeForces 1243"Character Swap (Hard Version)"(multimap)
传送门 •前置知识-multimap的用法 $multimap$ 与 $map$ 的区别在于一个 $key$ 可以对应几个值: 对于 $map$ 而言,一个 $key$ 只能对应一个值,并且按照 $k ...
- CodeForces - 1214D B2. Books Exchange (hard version)
题目链接:http://codeforces.com/problemset/problem/1249/B2 思路:用并查集模拟链表,把关系串联起来,如果成环,则满足题意.之后再用并查集合并一个链,一个 ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) B2. TV Subscriptions (Hard Version)
链接: https://codeforces.com/contest/1247/problem/B2 题意: The only difference between easy and hard ver ...
随机推荐
- ThinkPHP的几种路由形式总结
本篇文章总结了ThinkPHP的几种路由形式,分别是普通模式.PATHINFO.REWRITE和兼容模式,希望对各位学习thinkphp的朋友有帮助! thinkphp官方文档里有详细的说明: Thi ...
- Chrome80调整SameSite策略对IdentityServer4的影响以及处理方案(翻译)
首先,好消息是Goole将于2020年2月份发布Chrome 80版本.本次发布将推进Google的"渐进改良Cookie"策略,打造一个更为安全和保障用户隐私的网络环境. 坏消息 ...
- 《面试经典系列》- MySQL数据库存储引擎
一.MySQL有多少种存储引擎? 在MySQL5之后,支持的存储引擎有十多个,但是我们常用的就那么几种,而且,默认支持的也是 InnoDB. 通过命令:show engines \g,我们可以查看到当 ...
- excel中存储的icount,赋值完之后
最近需要实现一个功能,为了确保每次函数运行的时候count是唯一的,所以想读取excel中存储的icount,赋值完之后对其进行+1操作,并存入excel文件,确保下次读取的count是新的,没有出现 ...
- 在Windows Python3.4 上安装NumPy、Matplotlib、SciPy和IPython
NumPy 下载地址: http://sourceforge.net/projects/numpy/files/NumPy/1.8.1/ SciPy 下载地址: http://sourceforge. ...
- 移动端H5调试
背景:开发PC页面的时候使用chrome浏览器的开发者工具,可以很容易的捕获到页面的dom元素,并且可以修改样式,方便调试:但是手机上却很麻烦,因为手机上没有办法直接打开开发者工具查看元素.其实可以通 ...
- Springboot 事务注解--- @Transactional
spring boot @Transactional事物处理 spring boot 添加事物使用 @Transactional注解 简单使用 在启动类上方添加 @EnableTransacti ...
- python ——钟表
1.表盘 from turtle import * from datetime import * def Skip(step):#表盘不连续地画图 penup() forward(step) pend ...
- 【纯净镜像】原版Windows7集成USB3.0+NVME补丁+UEFI引导旗舰版下载
系统简述: 1. 基于MSDN原版Windows7 Ultimate With SP1系统制作,无任何插件和垃圾软件. 2. 系统集成IE11浏览器,装完系统后默认浏览器就是IE11. 3.系统注入了 ...
- 【学习笔记】CART算法
1. 背景介绍 CART(Classification and Regression Trees,分类回归树)算法是一种树构建算法,既可以用于分类,也可以用于回归.它的工作原理是:使用二元切分来处理连 ...