C. Swap Letters 01字符串最少交换几次相等
C. Swap Letters
2 seconds
256 megabytes
standard input
standard output
Monocarp has got two strings ss and tt having equal length. Both strings consist of lowercase Latin letters "a" and "b".
Monocarp wants to make these two strings ss and tt equal to each other. He can do the following operation any number of times: choose an index pos1pos1 in the string ss, choose an index pos2pos2 in the string tt, and swap spos1spos1 with tpos2tpos2.
You have to determine the minimum number of operations Monocarp has to perform to make ss and tt equal, and print any optimal sequence of operations — or say that it is impossible to make these strings equal.
The first line contains one integer nn (1≤n≤2⋅105)(1≤n≤2⋅105) — the length of ss and tt.
The second line contains one string ss consisting of nn characters "a" and "b".
The third line contains one string tt consisting of nn characters "a" and "b".
If it is impossible to make these strings equal, print −1−1.
Otherwise, in the first line print kk — the minimum number of operations required to make the strings equal. In each of the next kk lines print two integers — the index in the string ss and the index in the string tt that should be used in the corresponding swap operation.
4
abab
aabb
2
3 3
3 2
1
a
b
-1
8
babbaabb
abababaa
3
2 6
1 3
7 8
In the first example two operations are enough. For example, you can swap the third letter in ss with the third letter in tt. Then s=s= "abbb", t=t= "aaab". Then swap the third letter in ss and the second letter in tt. Then both ss and tt are equal to "abab".
In the second example it's impossible to make two strings equal.
题意:给你两个字符串,问最少需要交换多少次可以使这两个字符串相等,并且输出交换方案
题解:因为字符只有a,b两种;所以不相等的时候只有两种情况
1、
a
b
2、
b
a
分别统计这两种情况的出现次数,用k1,k2表示
如果k1,k2有一个为奇数,一个为偶数 ,即(k1+k2)%2==1,则不可能交换之后两字符串相等,输出 -1
否则 先让同一种不相等情况的先两两交换,交换次数为k1/2+k2/2
最后判断k1,k2是否都是奇数,如果是的话,最后只剩下如下一组不相等的情况
a b
b a
这里需要交换两次才能使两字符串相等
------------1
b b
a a
------------2
b a
b a
------------
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<stack>
#include<string.h>
#include<string>
#include<vector>
#define ll long long
using namespace std;
int pos1[],pos2[];
int main()
{
string s1,s2;
int t;
cin>>t;
cin>>s1;
cin>>s2; int k1=,k2=;
for(int i=;i<t;i++)
{
if(s1[i]!=s2[i]&&s1[i]=='a')
pos1[k1++]=i+;//输出下标是从1开始 if(s1[i]!=s2[i]&&s1[i]=='b')
pos2[k2++]=i+;
}
if((k1+k2)%==)//k1,k2一个为奇数,一个为偶数
cout<<-<<endl;
else
{
int cnt=k1/+k2/;
if(k1%==&&k2%==)//如果k1,k2为奇数最后交换的时候要交换两次
cnt=cnt+;
cout<<cnt<<endl;
int i,j;
for(i=;i+<k1;i=i+)
cout<<pos1[i]<<' '<<pos1[i+]<<endl; for(j=;j+<k2;j=j+)
cout<<pos2[j]<<' '<<pos2[j+]<<endl; if(i!=k1&&j!=k2)//处理最后一次交换下标
{
cout<<pos1[k1-]<<' '<<pos1[k1-]<<endl;
cout<<pos1[k1-]<<' '<<pos2[k2-]<<endl;
}
} return ;
}
C. Swap Letters 01字符串最少交换几次相等的更多相关文章
- hiho #1326 : 有序01字符串
#1326 : 有序01字符串 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于一个01字符串,你每次可以将一个0修改成1,或者将一个1修改成0.那么,你最少需要修改 ...
- hiho 有序01字符串 dp
题目1 : 有序01字符串 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于一个01字符串,你每次可以将一个0修改成1,或者将一个1修改成0.那么,你最少需要修改多少 ...
- 使序列有序的最少交换次数(minimum swaps)
交换相邻两数 如果只是交换相邻两数,那么最少交换次数为该序列的逆序数. 交换任意两数 数字的总个数减去循环节的个数?? A cycle is a set of elements, each of wh ...
- 深度优先搜索 codevs 1065 01字符串
codevs 1065 01字符串 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 输出仅有0和1组成的长度为n的字符串,并且 ...
- Codevs 1065 01字符串
1065 01字符串 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 输出仅有0和1组成的长度为n的字符串,并且其中不能含有 ...
- codevs——1065 01字符串
1065 01字符串 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 输出仅有0和1组成的长度为n的字符串, ...
- NPU 2015年陕西省程序设计竞赛网络预赛(正式赛)F题 和谐的比赛(递推 ||卡特兰数(转化成01字符串))
Description 今天西工大举办了一场比赛总共有m+n人,但是有m人比较懒没带电脑,另外的n个人带了电脑.不幸的是,今天机房的电脑全坏了只能用带的电脑,一台电脑最多两人公用,确保n>=m. ...
- P1071 01字符串的交叉安排
题目描述 你有 \(n(1 \le n \le 10^6)\) 个字符'0' 和 \(m(1 \le m \le 10^6)\) 个字符'1'.你需要使用这些字符拼接成一个01字符串,使得满足如下两个 ...
- Java实现 LeetCode 777 在LR字符串中交换相邻字符(分析题)
777. 在LR字符串中交换相邻字符 在一个由 'L' , 'R' 和 'X' 三个字符组成的字符串(例如"RXXLRXRXL")中进行移动操作.一次移动操作指用一个"L ...
随机推荐
- 第二次写linux驱动总结
第一次写驱动是在去年,2019年十月份左右.当时是看着韦老师的视频一步步完成的.其中经历了很多error.搭建环境花费了很多精力.时间来到了2020年2月19日星期三,韦老师新视频出来了,我跟着再来了 ...
- python-线程池的两种实现方式 【转载】
#!/usr/bin/env python # -*- coding:utf-8 -*- import queue import threading import contextlib import ...
- 使用php-vmstat遇到的麻烦
workerman-vmstat是一个基于workerman的扩展,用于监听服务器应用对内存.cpu消耗的友好的查看功能,具体介绍可以去git上看: https://github.com/wal ...
- eclipse修改快捷键
eclipse修改快捷键 ctrl + shift + l查看快捷键 window -> preferences -> 搜索keys 鼠标点击以下五个表头,可以按照内容搜索 示例,选中Bi ...
- java ArrayList添加元素全部一样
#开始 今天遇到了一个很神奇的事情 也即是我在用ArrayList的add方法循环加入对象的时候 发现添加的元素全部都是一样的 定位错误定位了一个下午.... 错误位置就是哪一个位置 但是就是不知道为 ...
- eclipse中使用maven update project功能后,默认又回到了jre 1.5的解决方案
在maven项目中的pom.xml中添加以下节点,进行jre版本的配置,配置完后再进行项目更新后,并不会自动切换到jre1.5 添加在pom的url标签后面 <build> ...
- Java web 会话技术 cookie与session
一.会话 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 会话过程中要解决的一些问题 每个用户在使用浏览器与服务器进行会话的过程 ...
- 传奇gee引擎,智能假人,假人脚本,geeM2假人
开新区注意事项: 1.新区无任何玩家数据下可以运行“MirServer\假人行会初始化”目录下的“点我初始化假人行会.Bat”程序 2.默认假人后台管理密码为:2139263 ;--------- ...
- Java生鲜电商平台-小程序或者APP优惠券的设计与源码实战
Java生鲜电商平台-小程序或者APP优惠券的设计与源码实战 说明:Java生鲜电商平台-小程序或者APP优惠券的设计与源码实战,优惠券是一种常见的促销方式,在规定的周期内购买对应商品类型和额度的商品 ...
- 10.Redis的RDB和AOF两种持久化机制的优劣势对比
1.RDB和AOF两种持久化机制的介绍 2.RDB持久化机制的优点3.RDB持久化机制的缺点4.AOF持久化机制的优点5.AOF持久化机制的缺点6.RDB和AOF到底该如何选择 我们已经知道对于一个企 ...