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划分为几个区间,每 ...
随机推荐
- obj.offsetHeight与obj.style.height $(obj).height()与$(obj).css('height')
相同:都可以获取obj的高度区别:(1)obj.offsetHeight可以获取外部.内嵌和内联中定义的高,而obj.style.height只能获取内联中定义的高:(2)obj.offsetHeig ...
- <iframe>框架标签的使用
同源下 1.iframe属性设置参考:https://blog.csdn.net/xiyiyindie/article/details/53415158 2.父子页面之间元素相互操作:https:// ...
- error C2065: CoInitializeEx' : undeclared identifier 解决方法
错误: error C2065: CoInitializeEx' : undeclared identifier 解决方法 原因: 本来程序的编译选项选择的是:使用标准windows库,当改为在静态库 ...
- JAVA JDBC大数据量导入Mysql
转自https://blog.csdn.net/q6834850/article/details/73726707?tdsourcetag=s_pctim_aiomsg 采用JDBC批处理(开启事务. ...
- SpringBoot项目框架下ThreadPoolExecutor线程池+Queue缓冲队列实现高并发中进行下单业务
主要是自己在项目中(中小型项目) 有支付下单业务(只是办理VIP,没有涉及到商品库存),目前用户量还没有上来,目前没有出现问题,但是想到如果用户量变大,下单并发量变大,可能会出现一系列的问题,趁着空闲 ...
- git分布式版本控制系统权威指南学习笔记(二):git add暂存区的三个状态以及暂存区的理解
文章目录 不经过git add(到暂存区),能直接进行commit吗? 举个
- TLS/SSL 协议 - ServerKeyExchange、ServerHelloDone
ServerKeyExchange ServerKeyExchange消息的目的是携带密钥交换的额外数据.消息内容对于不同的协商算法套件都会存在差异.在某些场景中,服务器不需要发送任何内容,这意味着在 ...
- 修改 Chrome浏览器主页被劫持 chrome 主页被篡改成hao.qquu8.com的解决方案
1. 开始菜单输入‘g'找到 Google Chrome浏览器 2. 修改属性->目标,将 chrome.exe hao.qquu8.com 后面的网址去掉,如图:
- Spring-boot整合Redis,遇到的问题
1.通过set进redis中的数据,get不到 String cityKey ="city_"+id; ValueOperations<String,City> ope ...
- 将时间 '2018-08-06T10:00:00.000Z' 格式转化为本地时间
参考:https://blog.csdn.net/sxf_123456/article/details/81582964 参考模板: from datetime import datetime, ti ...