链接:

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)的更多相关文章

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

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

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

  5. 【CF1243B2】Character Swap (Hard Version)【思维】

    题意:给定两个字符串,问是否存在交换方案使得两个字符串相同,方案为交换次数小于等于2n,且每次只交换s1与s2中的一个字符 题解:考虑从前往后枚举,当第i位不同时,考虑找后边的第j位,若存在这样的第j ...

  6. 【CF1243B1】Character Swap (Easy Version)【思维】

    题意:给你两个字符串,问是否存在交换方案使得两个字符串变成一样的,方案为只交换一次且只交换s1与s2里的一个字符 题解:若一开始就相同,则存在交换方案 若一开始不同的位置为1个或大于2个,则不存在方案 ...

  7. CodeForces 1243"Character Swap (Hard Version)"(multimap)

    传送门 •前置知识-multimap的用法 $multimap$ 与 $map$ 的区别在于一个 $key$ 可以对应几个值: 对于 $map$ 而言,一个 $key$ 只能对应一个值,并且按照 $k ...

  8. CodeForces - 1214D B2. Books Exchange (hard version)

    题目链接:http://codeforces.com/problemset/problem/1249/B2 思路:用并查集模拟链表,把关系串联起来,如果成环,则满足题意.之后再用并查集合并一个链,一个 ...

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

随机推荐

  1. 洛谷 P3808 【模板】AC自动机(简单版) 题解

    原题链接 前置知识: 字典树.(会 \(\texttt{KMP}\) 就更好) 显然呢,本题用 字典树 和 \(\texttt{KMP}\) 无法解决问题. 所以我们发明了一个东西: \(\textt ...

  2. 图片OCR(Optical Character Recognition)

    目录 Photo OCR问题描述 滑动窗口(Sliding Windows) 获得大量数据和人工数据(Getting Logs of Data and Artificial Data) 瓶颈分析:需要 ...

  3. WordPress 迁移站点更换域名为新域名

    使用 wp-cli 工具搜索替换域名的方式更换 WordPress 域名 wp-cli 是一个命令行工具,可以让我们通过命令行安装.更新 WordPress,对 WordPress 执行一些批量操作, ...

  4. Excel知识点与技巧1

    1.工作区:方便两个工作表之间进行对比 2.工作表标签颜色 3.交换两列的次序 4.快速到达边界:即快速到达第一行或最后一行 5.冻结窗格:可以固定某几行或某几列一直存在于窗口,不会随着往下拉或往右拉 ...

  5. 83 项开源视觉 SLAM 方案够你用了吗?

    作者:吴艳敏 来源:83 项开源视觉 SLAM 方案够你用了吗? 前言 1. 本文由知乎作者小吴同学同步发布于https://zhuanlan.zhihu.com/p/115599978/并持续更新. ...

  6. Unable to locate JAR/zip in file system as specified by the driver definitio

    把之前的驱动包删掉,然后把你的驱动包导入就行了 现在OK键就算正常了

  7. MATLAB——元胞数组

    一. 1.元胞数组的创建 >> a={;ones(,),:} a = ] [2x3 ;ones(,),:} >> b=[{};{ones(,)},{:}] b = ] [2x3 ...

  8. 求组合数m_n

    下面为求取组合数的代码: #include <stdio.h> #define MAX 10009 ]; void print(int *v, int length) { ; for (; ...

  9. A AK的距离

    时间限制 : - MS   空间限制 : - KB  评测说明 : 1s,128m 问题描述 同学们总想AK.于是何老板给出一个由大写字母构成的字符串,他想你帮忙找出其中距离最远的一对'A'和'K'. ...

  10. H - 覆盖的面积(线段树-线段扫描 + 离散化(板题))

    给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input 输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1 ...