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 题意:给你一个序列,你可以交换相邻的两个数,要达到一个要求,所有相同的数都相邻,问你交换次数最少是多少 思路 ...
随机推荐
- Linux (x86) Exploit 开发系列教程之七 绕过 ASLR -- 第二部分
(1)原理: 使用爆破技巧,来绕过共享库地址随机化.爆破:攻击者选择特定的 Libc 基址,并持续攻击程序直到成功.这个技巧是用于绕过 ASLR 的最简单的技巧. (2)漏洞代码 //vuln.c # ...
- Vue路由系统
Vue路由系统 一切分离都是为了更好的结合,本文详细介绍了前后端分离架构之后,前端如何实现路由控制,即Vue路由系统. 一.VueRouter实现原理 VueRouter的实现原理是根据监控锚点值的改 ...
- scratch少儿编程第一季——05、移动还可以这样动
各位小伙伴大家好: 上期我们学习了怎么控制方向和移动的程序块. 今天我们继续学习运动模块下的其他9个指令(程序块). 首先来看前面两个关于x坐标的程序块. 分别是将x坐标增加()单位,和将x坐标设定为 ...
- JS 04 Date_Math_String_Object
Date <script> //1.Date对象 var d1 = new Date(); //Thu May 02 2019 14:27:19 GMT+0800 (中国标准时间) con ...
- java字节和字符的区别
字节: 1.bit=1 二进制数据0或1 2.byte=8bit 1个字节等于8位 存储空间的基本计量单位 3.一个英文字母=1byte=8bit 1个英文字母是1个字节,也就是8位 4.一个汉字 ...
- Python 的 Mixin 类(转)
转1:https://www.cnblogs.com/aademeng/articles/7262520.html 转2:https://blog.csdn.net/u010377372/articl ...
- C#利用反射和泛型给不同对象赋值
/// <summary> /// 适用于初始化新实体 /// </summary> static public T RotationMapping<T, S>(S ...
- (五)Redis之List
一.List常用命令 两端添加 两端弹出 1.2. 两端添加和两端弹出 package myRedis01; import java.util.HashMap; import java.util.Li ...
- Nginx与负载均衡
Nginx,首先是一款轻量级的Web服务器,其特点是占有内存少,并发能力强,大厂用户有:百度.新浪.网易.腾讯等.其次,它是一款反向代理服务器:第三,它还是一款电子邮件(IMAP/POP3)代理服务器 ...
- python3爬虫之requests库基本使用
官方文档链接(中文) https://2.python-requests.org/zh_CN/latest/ requests 基于 urllib3 ,python编写. 安装 pip insta ...