CodeForces-1215C-Swap Letters-思维
Monocarp has got two strings ss and tt having equal length. Both strings consist of lowercase Latin letters "a" and "b".
Monocarp wants to make these two strings ss and tt equal to each other. He can do the following operation any number of times: choose an index pos1pos1 in the string ss, choose an index pos2pos2 in the string tt, and swap spos1spos1 with tpos2tpos2.
You have to determine the minimum number of operations Monocarp has to perform to make ss and tt equal, and print any optimal sequence of operations — or say that it is impossible to make these strings equal.
The first line contains one integer nn (1≤n≤2⋅105)(1≤n≤2⋅105) — the length of ss and tt.
The second line contains one string ss consisting of nn characters "a" and "b".
The third line contains one string tt consisting of nn characters "a" and "b".
If it is impossible to make these strings equal, print −1−1.
Otherwise, in the first line print kk — the minimum number of operations required to make the strings equal. In each of the next kk lines print two integers — the index in the string ss and the index in the string tt that should be used in the corresponding swap operation.
4
abab
aabb
2
3 3
3 2
1
a
b
-1
8
babbaabb
abababaa
3
2 6
1 3
7 8
In the first example two operations are enough. For example, you can swap the third letter in ss with the third letter in tt. Then s=s= "abbb", t=t= "aaab". Then swap the third letter in ss and the second letter in tt. Then both ss and tt are equal to "abab".
In the second example it's impossible to make two strings equal.
题意:
给出s和t两个长度相等的字符串,问需要交换多少次s和t才能相等
输出次数和需要交换的下标
思路:
分两种情况进行讨论
ababab
babbab
ab=2,ba=1
所以ab+ba=3
即偶+奇=奇(直接输出-1即可)
可以交换成功的,进行分类讨论:
只有ab+ba的和为偶数才可以交换成功,
而出现偶数的可能只有偶+偶=偶或者奇+奇=偶
故此时可以进行下一步判断哪个奇数哪个偶数
abababb
babbaba
ab=2,ba=2
所以ab+ba=4
即偶+偶=偶
所以ab和ab之间交换,ba和ba之间进行交换
abababbab
babbababa
ab=3,ba=3
所以ab+ba=6
即奇+奇=偶
把多余的那一项拿出来单独进行交换即可
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int N=2e5+;
char s[N],t[N];
int ab[N],ba[N];//记录下标 int main()
{
int n;
while(~scanf("%d",&n))
{
scanf("%s%s",s,t);
// int ab=0,ba=0;
// for(int i=0;i<n;i++)
// {
// if(s[i]=='a'&&t[i]=='b')
// ab++;
// else if(s[i]=='b'&&t[i]=='a')
// ba++;
// else
// continue;
// }
// if(ab!=ba)
// printf("-1\n");//s=ab、t=ab这样子不对了就
int p=,q=;
for(int i=; i<n; i++)
{
if(s[i]=='a'&&t[i]=='b')
ab[p++]=i+;
else if(s[i]=='b'&&t[i]=='a')
ba[q++]=i+;
else
continue;
}
p--,q--;
int sum=p+q;
if(sum%)//如果和是奇数
{
printf("-1\n");
continue;
}
if(p%&&q%)
//即奇+奇=偶
//把多余的那一项拿出来单独进行交换即可
{
int kk=(p-)/+(q-)/+;
printf("%d\n",kk+);
for(int i=;i<=p-;i+=)
printf("%d %d\n",ab[i],ab[i+]);
for(int i=;i<=q-;i+=)
printf("%d %d\n",ba[i],ba[i+]);
printf("%d %d\n%d %d\n",ba[q],ba[q],ba[q],ab[p]);
}
else if(p%==&&q%==)
//即偶+偶=偶
//所以ab和ab之间交换,ba和ba之间进行交换
{
printf("%d\n",(p+q)/);
for(int i=;i<=p;i+=)
printf("%d %d\n",ab[i],ab[i+]);
for(int i=;i<=q;i+=)
printf("%d %d\n",ba[i],ba[i+]);
}
}
return ;
}
CodeForces-1215C-Swap Letters-思维的更多相关文章
- Codeforces 1215C. Swap Letters
传送门 好像是个挺显然的贪心 首先每次交换当然要尽量一次交换就多两个相同的位置 即 优先把 $\begin{bmatrix}a\\ b\end{bmatrix}$ 和 $\begin{bmatrix} ...
- C. Swap Letters 01字符串最少交换几次相等
C. Swap Letters time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #585 (Div. 2) C. Swap Letters
链接: https://codeforces.com/contest/1215/problem/C 题意: Monocarp has got two strings s and t having eq ...
- Codeforces Round 56-B. Letters Rearranging(思维)
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- Educational Codeforces Round 60 C 思维 + 二分
https://codeforces.com/contest/1117/problem/C 题意 在一个二维坐标轴上给你一个起点一个终点(x,y<=1e9),然后给你一串字符串代表每一秒的风向, ...
- Educational Codeforces Round 61 F 思维 + 区间dp
https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...
- [Codeforces 1178D]Prime Graph (思维+数学)
Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...
- Sorted Adjacent Differences(CodeForces - 1339B)【思维+贪心】
B - Sorted Adjacent Differences(CodeForces - 1339B) 题目链接 算法 思维+贪心 时间复杂度O(nlogn) 1.这道题的题意主要就是让你对一个数组进 ...
- Codeforces Problem 708A Letters Cyclic Shift
题目链接: http://codeforces.com/problemset/problem/708/A 题目大意: 从字符串s中挑选出一个子串(非空),将该子串中的每个字母均替换成前一个字母,如' ...
- Codeforces 675C Money Transfers 思维题
原题:http://codeforces.com/contest/675/problem/C 让我们用数组a保存每个银行的余额,因为所有余额的和加起来一定为0,所以我们能把整个数组a划分为几个区间,每 ...
随机推荐
- 详解Windows注册表分析取证
大多数都知道windows系统中有个叫注册表的东西,但却很少有人会去深入的了解它的作用以及如何对它进行操作.然而对于计算机取证人员来说注册表无疑是块巨大的宝藏.通过注册表取证人员能分析出系统发生了什么 ...
- RN 0.6以后react-navigation 导航报错null is not an object (evaluating '_RNGestureHandlerModule.default.Direction')
很久没用RN了之前用的时候还是很早的版本,现在有了很大的变化,感觉比之前成熟了很多重新看下.先整下导航 参考地址:https://reactnavigation.org/docs/en/getting ...
- express简易代理请求
var express = require('express') var app = express() var proxy = require('http-proxy-middleware') va ...
- rbd_rados命令拷屏
mimic或者luminous rbd_rados sudo mount -t ceph 192.168.7.151:6789:/ /mnt -o name=admin,secret=AQBaPZNc ...
- Vim用法AAAAA
.linux系统中如何进入退出vim编辑器,方法及区别 我们当然要保存并退出了,然后下一步了.这时,我们要按键盘左上角的"ESC",留意到了没有?左下角的插入状态不见了,如图. 然 ...
- Java进程Runtime、Process、ProcessBuilder调用外部程序
原文地址:https://blog.csdn.net/c315838651/article/details/72085739 通过Java执行系统命令,与cmd中或者终端上一样执行shell命令,最典 ...
- python_way ,day22 tonardo,jsonp
python_way day22 1.tonardo 2.cookie 3.api认证 一.tonardo: a.tonardo 初识 #!/usr/bin/env python3# Created ...
- build protobuf to a static library
use ar cd src ar -cvq libprotobuf.a *.o
- 正则化:L0 vs L1 vs L2
原文地址:https://www.jianshu.com/p/e5c9a9fc84d4 为什么正则化可以缓解过拟合? 过拟合时,拟合函数的系数往往非常大.过大的权重会导致模型过多地学习到某些数据的个性 ...
- 并发编程之CAS(二)
更多Android架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从以下几个内容来阐述CAS: [CAS原理] [CAS带来的ABA问题] 一 ...