Codeforces Round #585 (Div. 2) C. Swap Letters
链接:
https://codeforces.com/contest/1215/problem/C
题意:
Monocarp has got two strings s and t having equal length. Both strings consist of lowercase Latin letters "a" and "b".
Monocarp wants to make these two strings s and t equal to each other. He can do the following operation any number of times: choose an index pos1 in the string s, choose an index pos2 in the string t, and swap spos1 with tpos2.
You have to determine the minimum number of operations Monocarp has to perform to make s and t equal, and print any optimal sequence of operations — or say that it is impossible to make these strings equal.
思路:
考虑三种情况, (a, b)(a, b), 这样一步就能交换成(a, a)(b, b), (b, a)(b, a)同理.
(a, b)(b, a)这种情况先要交换成(a, a)(b, b)再交换, 多一步.
记录(a, b)和(b, a)的数量, 总和需为偶数.
再组内两两匹配, 如果多出来就多一步.
代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+10;
char s[MAXN], t[MAXN];
vector<int > a, b;//a b, b a
int n;
int main()
{
cin >> n;
cin >> s >> t;
for (int i = 0;i < n;i++)
{
if (s[i] == t[i])
continue;
if (s[i] == 'a')
a.push_back(i+1);
if (s[i] == 'b')
b.push_back(i+1);
}
if ((a.size()+b.size())%2)
{
puts("-1");
return 0;
}
int cnt = (a.size()+b.size())/2;
if (a.size()%2)
cnt++;
cout << cnt << endl;
for (int i = 0;i+1 < a.size();i +=2)
cout << a[i] << ' ' << a[i+1] << endl;
for (int i = 0;i+1 < b.size();i += 2)
cout << b[i] << ' ' << b[i+1] << endl;
if (a.size()%2)
{
cout << *a.rbegin() << ' ' << *a.rbegin() << endl;
cout << *a.rbegin() << ' ' << *b.rbegin() << endl;
}
return 0;
}
Codeforces Round #585 (Div. 2) C. Swap Letters的更多相关文章
- Codeforces Round #585 (Div. 2)
https://www.cnblogs.com/31415926535x/p/11553164.html 感觉很硬核啊这场,,越往后越做不动,,,emmmm,,,(这场是奔着最后一题 2sat 来的, ...
- Codeforces Round #585 (Div. 2) [补题]
前言 2019.9.16 昨天下午就看了看D题,没有写对,因为要补作业,快点下机了,这周争取把题补完. 2019.9.17 这篇文章或者其他文章难免有错别字不被察觉,请读者还是要根据意思来读,不要纠结 ...
- Codeforces Round #585 (Div. 2) CF1215A~C
CF1215A. Yellow Cards简单的模拟,给定了黄票张数,判断最少和最多有多少人被罚下场. #include <bits/stdc++.h> using namespace s ...
- Codeforces Round #585 (Div. 2) D. Ticket Game
链接: https://codeforces.com/contest/1215/problem/D 题意: Monocarp and Bicarp live in Berland, where eve ...
- Codeforces Round #585 (Div. 2) A. Yellow Cards(数学)
链接: https://codeforces.com/contest/1215/problem/A 题意: The final match of the Berland Football Cup ha ...
- B. The Number of Products(Codeforces Round #585 (Div. 2))
本题地址: https://codeforces.com/contest/1215/problem/B 本场比赛A题题解:https://www.cnblogs.com/liyexin/p/11535 ...
- A. Yellow Cards ( Codeforces Round #585 (Div. 2) 思维水题
---恢复内容开始--- output standard output The final match of the Berland Football Cup has been held recent ...
- Codeforces Round #585 (Div. 2) B. The Number of Products(DP)
链接: https://codeforces.com/contest/1215/problem/B 题意: You are given a sequence a1,a2,-,an consisting ...
- Codeforces Round #585 (Div. 2) E. Marbles (状压DP)
题目:https://codeforc.es/contest/1215/problem/E 题意:给你一个序列,你可以交换相邻的两个数,要达到一个要求,所有相同的数都相邻,问你交换次数最少是多少 思路 ...
随机推荐
- 《Mysql - SQL优化》
一:在查询语句时,应该注意的优化问题 - SELECT语句务必指明字段名称 - SELECT * 会增加很多不必要的消耗(CPU.IO.内存.网络带宽) - 同时会让 Mysql 优化器无法优化 - ...
- Scratch第四十九讲:完美的下落和反弹
做了很多小游戏,都会遇到碰撞和反弹的情况,CC哥大多时候也都是简单处理一下,包括之前的讲座也有提过,但是没有认真的讲解过.今天就专门为这个主题做一讲,把这部分内容彻底讲透,大家可以一起探讨一下. 是不 ...
- (转) 从0移植uboot(五) _实现串口输出
ref : https://www.cnblogs.com/xiaojiang1025/p/6500520.html 串口作为一种非常简单的通信方式,才是嵌入式系统调试的王道,通过设置串口输出,我们可 ...
- Redis过期命令
Redis键的过期时长的设定 ·命令名称:EXPIRE ·语法:EXPIRE key seconds ·功能:为给定key设置生存时间,当key过期时(生存时间为0),它会被自动删除 ·返回值:设置成 ...
- Unity塔防游戏的创建
看了下塔防游戏的教程,比我想像的还简单一些,有些收获: (1)敌人的移动路径,其时比较简单,用了N个Empty GameObject作为路径点,然后做一个总的Empty GameObject 作为父级 ...
- HTML中关于动态创建的标签无法绑定js事件的解决方法:.on()方法的 [.selector]
在前端页面的时候,会经常遇到用JavaScript动态创建出来的Button按钮或其他标签无法使用点击事件的问题.如下代码,使用jquery在body中动态创建一个class为demo的Button按 ...
- .net core partial view的一些心得
原文:.net core partial view的一些心得 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog. ...
- Uwl.Admin开源框架(一)
1.前言 作为一个忠实的软粉,一直期待微软出跨平台,一直在等待.Net Core,因为刚毕业对于.Net的很多东西不是很熟知,就开始了.Net Core的摸索,一路上坎坎坷坷,对于新技术一直很期待,就 ...
- 8.读写锁ReadWriteLock
/*ReadWriteLock 读写锁*/ private ReadWriteLock lock = new ReentrantReadWriteLock(); lock.readLock().loc ...
- dubbo常见异常及解决方式
1.出现RpcException: Failed to invoke the method post in the service com.xxx.xxx.xxx异常怎么办?表示调用失败1.检查网络是 ...