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 题意:给你一个序列,你可以交换相邻的两个数,要达到一个要求,所有相同的数都相邻,问你交换次数最少是多少 思路 ...
随机推荐
- 暴力解决HTMLTestRunner出现dict() ->new empty dictionary的问题
在使用ddt数据驱动之后,HTMLTestRunner报告的用例名称之后出现dict() ->new empty dictionary,如下图所示: 经排查,发现是因为ddt源码中test_do ...
- 《Mysql - SQL优化》
一:在查询语句时,应该注意的优化问题 - SELECT语句务必指明字段名称 - SELECT * 会增加很多不必要的消耗(CPU.IO.内存.网络带宽) - 同时会让 Mysql 优化器无法优化 - ...
- js — 基础知识
目录 1. js的介绍 2. js的引入方式 3. js语句 4. 变量 js ( Javascript ) - 脚本语言 1. js的介绍 Javascript是一种运行在浏览器中的解释型的编程语言 ...
- 5-6 c语言之【枚举,联合体,递归】
今天学习了枚举,联合体,递归,重点在于递归,所以从网上找到了一些递归的题目进行练习,毕竟程序员界流传一句话,会用循环的是人,会用递归的是神,哈哈哈 还是按次序进行梳理,第一个枚举,枚举和宏定义很相似, ...
- Layui连接mysql操作CRUD案例
今天分享的是一个新前端框架Layui,用它来链接数据库实现一下crud的操作. 一:layui简历 layui,是一款采用自身模块规范编写的前端 UI 框架,遵循原生 HTML/CSS/JS 的书写与 ...
- UOJ208 UOIP十合一(提交答案)
首先对每张图都去掉自环. 1:给出的就是DAG.答案即为2m. 2.5:显然每个SCC之间互相独立.这两个点都满足SCC中的点很少.于是对每个SCC暴力枚举边集判环,而SCC之间的边显然选不选没有影响 ...
- 解决django的后台管理界面添加中文内容乱码问题
在使用django migrate功能时,默认数据库的字符集不是utf8. 是latin 1,然后在后台管理model时,不允许有中文字符插入 解决方案: 在使用migrate建库之前先把数据库建立起 ...
- 将自定义dockerfile生成的image推送到仓库中
本文为以阿里云的案例方法,其他方法未尝试 1.注册阿里云账号 2.设置密码并通过docker登录 3.创建命名空间 4.创建镜像仓库 创建仓库时,需要选择代码源的仓库储存的方式,这里我用的是gitHu ...
- HTTP协议探究(五):H2中一些重要的概念
一 复习与目标 1 复习 简单密码学.对称加密与非对称加密 数字签名.数字证书 SSL/TLS HTTPS = HTTP + SSL/TLS,SSL/TLS为HTTP提供了保密性.完整性和鉴别性 2 ...
- 验证码识别的免费 OCR
在做接口自动化以及爬虫的过程中,验证码一般是个很烦的存在,其实大厂们已经做好了一些 OCR 供使用,这里介绍一下百度 OCR 的使用方法. 注册并生成应用 1.注册一个百度智能云账号:http://a ...