链接:

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的更多相关文章

  1. Codeforces Round #585 (Div. 2)

    https://www.cnblogs.com/31415926535x/p/11553164.html 感觉很硬核啊这场,,越往后越做不动,,,emmmm,,,(这场是奔着最后一题 2sat 来的, ...

  2. Codeforces Round #585 (Div. 2) [补题]

    前言 2019.9.16 昨天下午就看了看D题,没有写对,因为要补作业,快点下机了,这周争取把题补完. 2019.9.17 这篇文章或者其他文章难免有错别字不被察觉,请读者还是要根据意思来读,不要纠结 ...

  3. Codeforces Round #585 (Div. 2) CF1215A~C

    CF1215A. Yellow Cards简单的模拟,给定了黄票张数,判断最少和最多有多少人被罚下场. #include <bits/stdc++.h> using namespace s ...

  4. Codeforces Round #585 (Div. 2) D. Ticket Game

    链接: https://codeforces.com/contest/1215/problem/D 题意: Monocarp and Bicarp live in Berland, where eve ...

  5. Codeforces Round #585 (Div. 2) A. Yellow Cards(数学)

    链接: https://codeforces.com/contest/1215/problem/A 题意: The final match of the Berland Football Cup ha ...

  6. 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 ...

  7. A. Yellow Cards ( Codeforces Round #585 (Div. 2) 思维水题

    ---恢复内容开始--- output standard output The final match of the Berland Football Cup has been held recent ...

  8. 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 ...

  9. Codeforces Round #585 (Div. 2) E. Marbles (状压DP)

    题目:https://codeforc.es/contest/1215/problem/E 题意:给你一个序列,你可以交换相邻的两个数,要达到一个要求,所有相同的数都相邻,问你交换次数最少是多少 思路 ...

随机推荐

  1. [转帖]postgres csv日志和查看用户权限

    postgres csv日志和查看用户权限 最近在使用postgres 时遇到的2个问题,顺便记录一下查到的比较好的资料. 怀疑postgres在执行SQL时报错,程序日志中有无明确异常信息.通过查看 ...

  2. Redis 集群_主从复制_哨兵模型

    1 redis集群简介 1.1 集群的概念 所谓的集群,就是通过添加服务器的数量,提供相同的服务,从而让服务器达到一个稳定.高效的状态. 1.1.1 使用redis集群的必要性 问题:我们已经部署好了 ...

  3. GC(Garbage Collection)

    GC(Garbage Collection) GC背景 ​  创建对象会消耗内存,如果不回收对象占用的内存,内存使用率会越来越高,最终出现OutOfMemoryError(OOM) ​  在C++中专 ...

  4. python 正则 re模块(详细版)

    正则表达式 什么是正则表达式? 正则表达式是对字符串(包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为“元字符”))操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合 ...

  5. Kubernetes 学习笔记(五):数据卷

    "数据卷"通常和"有状态"这个词同时出现,卷用于给有状态应用保存/共享状态. 常用的数据卷类型 1. emptyDir: 用于存储临时数据的空目录 emptyD ...

  6. AtCoder练习

    1. 3721 Smuggling Marbles 大意: 给定$n+1$节点树, $0$为根节点, 初始在一些节点放一个石子, 然后按顺序进行如下操作. 若$0$节点有石子, 则移入盒子 所有石子移 ...

  7. 安装Docker step by step

    1. 系统要求 centos7以上   使用cat /etc/redhat-release查看系统版本,我的Centos 7.6 centos-extra 仓库 enable,默认是打开的 2.安装d ...

  8. SQL 不同服务器数据库操作

    https://www.cnblogs.com/lusunqing/p/3660190.html --创建远程链接服务器 execute sys.sp_addlinkedserver @server= ...

  9. JS 长按 移动端

    实质上,长按的时间不应该过长,因为这有可能与手机系统的部分长按手势产生冲突,但也不宜过短,因为长按时间过短与点击没有任何区别, 理论上,判断长按结束,在手机端上仅设置mouseup动作就可以, < ...

  10. PHP代码多人开发

    我们一定遇到过这样的情况:许多项目是由多个人同时开发的,涉及到不同的开发环境和测试环境我们的许多PHP工程师在windows上开发代码虽然nginx+PHP+MySQL的环境也可以在windows上配 ...