链接:

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. Python turtle(介绍一)

    关于绘制图形库turtle # 画布上,默认有一个坐标原点为画布中心的坐标轴(0,0),默认"standard"模式坐标原点上有一只面朝x轴正方向小乌龟 一:海龟箭头Turtle相 ...

  2. Scratch与物理·天文:模拟中国嫦娥探月工程,探索月球的背面!

    北京时间2019年5月16日凌晨,国际顶级学术期刊<自然>(Nature)在线发表了一篇来自中国科学家的成果:中国的嫦娥四号月球探测器2019年1月3日在月球背面的冯卡门陨石坑(Von K ...

  3. js 监听键盘的enter键

    // js 版本 window.onload=function(){ document.onkeydown=function(ev){ var event=ev ||event if(event.ke ...

  4. 整理下线段树吧 poj hotel

    除了上次的新学的有 区间更新 延迟更新  区间合并 先说下区间更新以及延迟更新吧 既然是对区间的维护 在求解一些问题的时候 有的时候没有必要对所有的自区间都进行遍历 这个时候 延迟标记就派上用场了 ( ...

  5. (十六)Activitivi5之内置用户组(角色)设计表以及IdentityService

    一.内置用户组(角色)设计表概念 用户和组(或者叫做角色),多对多关联,通过关联表实现 act_id_user 用户表: act_id_group 用户组表: act_id_membership 用户 ...

  6. Java QuickSort 快速排序

    Java QuickSort /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternational ...

  7. Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor'

    An unhandled exception occurred while processing the request. InvalidOperationException: Unable to r ...

  8. .net core partial view的一些心得

    原文:.net core partial view的一些心得 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog. ...

  9. JDBC 学习复习9 配置Tomcat数据源

    在实际开发中,我们有时候还会使用服务器提供给我们的数据库连接池,比如我们希望Tomcat服务器在启动的时候可以帮我们创建一个数据库连接池,那么我们在应用程序中就不需要手动去创建数据库连接池,直接使用T ...

  10. Google Drive ubuntu

    Google尚未发布用于从Ubuntu访问其drive的官方Linux客户端.然开源社区却业已开发完毕非官方之软件包‘grive-tools’. grive乃是Google Drive(在线存储服务) ...