Codeforces 1215C. Swap Letters
好像是个挺显然的贪心
首先每次交换当然要尽量一次交换就多两个相同的位置
即
优先把 $\begin{bmatrix}a\\ b\end{bmatrix}$ 和 $\begin{bmatrix}a\\ b\end{bmatrix}$ 交换
优先把 $\begin{bmatrix}b\\ a\end{bmatrix}$ 和 $\begin{bmatrix}b\\ a\end{bmatrix}$ 交换
最后如果剩下$\begin{bmatrix}a\\ b\end{bmatrix}$,$\begin{bmatrix}b\\ a\end{bmatrix}$ 各一个
我们才只好用两次交换次数把它们搞好,这样就是最优的了
当然如果最后某一种情况剩下一个,另一种情况却没了
那就无解了,具体维护看代码吧
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=4e5+;
int n,pa,pb;
char A[N],B[N];
vector <int> ans[];
int main()
{
n=read(); scanf("%s",A+); scanf("%s",B+);
for(int i=;i<=n;i++)
{
if(A[i]==B[i]) continue;
if(A[i]=='a')
{
if(!pa) pa=i;
else ans[].push_back(pa),ans[].push_back(i),pa=;
}
else
{
if(!pb) pb=i;
else ans[].push_back(pb),ans[].push_back(i),pb=;
}
}
if((pa&&!pb)||(!pa&&pb)) { printf("-1\n"); return ; }
if(pa&&pb) { ans[].push_back(pa); ans[].push_back(pa); ans[].push_back(pa); ans[].push_back(pb); }
int len=ans[].size();
printf("%d\n",len);
for(int i=;i<len;i++) printf("%d %d\n",ans[][i],ans[][i]);
return ;
}
Codeforces 1215C. Swap Letters的更多相关文章
- C. Swap Letters 01字符串最少交换几次相等
C. Swap Letters time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #585 (Div. 2) C. Swap Letters
链接: https://codeforces.com/contest/1215/problem/C 题意: Monocarp has got two strings s and t having eq ...
- Codeforces Problem 708A Letters Cyclic Shift
题目链接: http://codeforces.com/problemset/problem/708/A 题目大意: 从字符串s中挑选出一个子串(非空),将该子串中的每个字母均替换成前一个字母,如' ...
- codeforces 709C C. Letters Cyclic Shift(贪心)
题目链接: C. Letters Cyclic Shift 题意: 现在一串小写的英文字符,每个字符可以变成它前边的字符即b-a,c-a,a-z这样,选一个字串变换,使得得到的字符串字典序最小; 思路 ...
- Codeforces 899 F. Letters Removing (二分、树状数组)
题目链接:Letters Removing 题意: 给你一个长度为n的字符串,给出m次操作.每次操作给出一个l,r和一个字符c,要求删除字符串l到r之间所有的c. 题解: 看样例可以看出,这题最大的难 ...
- Codeforces 1009G Allowed Letters 最大流转最小割 sosdp
Allowed Letters 最直观的想法是贪心取, 然后网络流取check可不可行, 然后T了. 想到最大流可以等于最小割, 那么我们状压枚举字符代表的6个点连向汇点是否断掉, 然后再枚举64个本 ...
- CodeForces - 920C Swap Adjacent Elements
传送门:点我 You have an array a consisting of n integers. Each integer from 1 to n appears exactly once i ...
- Codeforces Round 56-B. Letters Rearranging(思维)
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- [Jenkins]Job中如何传递自定义变量
场景一: Job构建步骤间的变量传递 Jenkins提供了数十种构建方式,我们以最常用的『Execute shell』为例.有时为了使Job中的复杂的构建流程更加清晰我们配置多个构建步骤像下面这样.图 ...
- AbpUser 扩展
AbpUser表存放的信息比较少,现扩展一下信息 1.在Core层添加UserExtend 类,继承 AbpUser<User>,写入以上各项属性字段,并添加Discriminator 字 ...
- STS插件_ springsource-tool-suite插件各个历史版本
目前spring官网(http://spring.io/tools/sts/all)上可下载的spring插件只有:springsource-tool-suite-3.8.4(sts-3.8.4).但 ...
- [python 笔记] __iter__迭代器
可迭代对象和迭代器 https://blog.csdn.net/nightcharm/article/details/78964676 可迭代对象 对象里面含有__iter__()方法的实现,对象的_ ...
- [JZOJ6400]:Game(贪心+线段树+二分)
题目描述 小$A$和小$B$在玩一个游戏,他们两个人每人有$n$张牌,每张牌有一个点数,并且在接下来的$n$个回合中每回合他们两人会分别打出手中的一张牌,点数严格更高的一方得一分,然而现在小$A$通过 ...
- 在oracle数据库里创建自增ID字段的步骤
CREATE TABLE ADVICE ( ID INT NOT NULL, ACTIVE NOT NULL, TYPE INT NOT NULL, MSG ) NOT NULL, ADVICE ) ...
- leetcode-easy-others-190. Reverse Bits-NO
mycode 不会... 参考: 1. 思路: 将十进制的n转换成二进制(str) -> 利用切片.反向获取不包含0b的反转后的二进制字符串 -> 补上0(共32位) 2. class S ...
- N2RR
一.背景 氨(NH 3)是一种新兴的能量载体,在液氨中含有17.6%(重量)的氢,而在甲醇中的含量为12.5%(重量),很可能成为未来氢能经济的有希望的候选者.然而,如果基于NH 3的肥料不足以养活世 ...
- xstream解析xml时遇到特殊字符出错
在xml中有"&"符号时,解析xml出错 解决办法: 将&替换成&
- Echarts 里面获取纵坐标刻度的间距
概述 今天 PM 说,需要把 echarts 图表的纵坐标调成这样:如果全是 4 位数就用 K 为单位.冷静分析,就是说如果纵坐标刻度的间距是四位数,就用 K 为单位.那要如何获取纵坐标刻度的间距呢? ...