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 ...
随机推荐
- Mybatis在xml配置文件中处理SQL中的大于小于号的方法
之前在项目中遇到了在SQL中<=不识别的问题,在Navicat中语句正常,在xml中不识别,想起来就记录一下 项目用的是Mybatis,经过网上多次查询,验证,总结方法如下: 一.使用<! ...
- Python批量修改Excel中的文件内容
import osimport xlrdfrom xlutils.copy import copydef base_dir(filename=None): return os.path.join ...
- Hinton老爷子CapsNet再升级,结合无监督,接近当前最佳效果
2017 年,Geoffrey Hinton 在论文<Dynamic Routing Between Capsules>中提出 CapsNet 引起了极大的关注,同时也提供了一个全新的研究 ...
- Python第六章-函数06-高阶函数
函数的高级应用 二.高阶函数 高级函数, 英文叫 Higher-order Function. 那么什么是高阶函数呢? 在说明什么是=高阶函数之前, 我们需要对函数再做进一步的理解! 2.1 函数的本 ...
- Python3实现xml转json文件
使用了Python的 xml.etree.ElementTree 库,Python版本Python 3.6.6 from xml.etree import ElementTree LISTTYPE = ...
- iPhone连接到Mac上叮叮叮断断续续响个不停的解决办法
一.推荐方式 1.让iPhone和Mac通过数据线连接(对,就是连着) 2.打开终端,执行如下命令: sudo killall -STOP -c usbd 3.一分钟内,iPhone即可连上Mac 二 ...
- 将Python执行代码打包成exe可执行文件
安装pyinstaller pip3 install pyinstaller 进入py文件目录,执行以下指令 pyinstaller -F -w <文件名.py>,-F代表生成可执行文件, ...
- 基于.NetCore3.1搭建项目系列 —— 使用Swagger导出文档 (番外篇)
前言 回顾之前的两篇Swagger做Api接口文档,我们大体上学会了如何在net core3.1的项目基础上,搭建一套自动生产API接口说明文档的框架. 本来在Swagger的基础上,前后端开发人员在 ...
- .Net微服务实践(二):Ocelot介绍和快速开始
目录 介绍 基本原理 集成方式 快速开始 创建订单服务 创建产品服务 创建网关 运行验证 最后 上篇.Net微服务实践(一):微服务框架选型 我们对微服务框架整体做了介绍,接下来我们从网关Ocelot ...
- 利用Python批量重命名文件夹下文件
#!/usr/bin/python # -*- coding: UTF-8 -*- # -*- coding:utf8 -*- import os from string import digits ...