传送门

好像是个挺显然的贪心

首先每次交换当然要尽量一次交换就多两个相同的位置

优先把 $\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的更多相关文章

  1. C. Swap Letters 01字符串最少交换几次相等

    C. Swap Letters time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

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

  3. Codeforces Problem 708A Letters Cyclic Shift

     题目链接: http://codeforces.com/problemset/problem/708/A 题目大意: 从字符串s中挑选出一个子串(非空),将该子串中的每个字母均替换成前一个字母,如' ...

  4. codeforces 709C C. Letters Cyclic Shift(贪心)

    题目链接: C. Letters Cyclic Shift 题意: 现在一串小写的英文字符,每个字符可以变成它前边的字符即b-a,c-a,a-z这样,选一个字串变换,使得得到的字符串字典序最小; 思路 ...

  5. Codeforces 899 F. Letters Removing (二分、树状数组)

    题目链接:Letters Removing 题意: 给你一个长度为n的字符串,给出m次操作.每次操作给出一个l,r和一个字符c,要求删除字符串l到r之间所有的c. 题解: 看样例可以看出,这题最大的难 ...

  6. Codeforces 1009G Allowed Letters 最大流转最小割 sosdp

    Allowed Letters 最直观的想法是贪心取, 然后网络流取check可不可行, 然后T了. 想到最大流可以等于最小割, 那么我们状压枚举字符代表的6个点连向汇点是否断掉, 然后再枚举64个本 ...

  7. CodeForces - 920C Swap Adjacent Elements

    传送门:点我 You have an array a consisting of n integers. Each integer from 1 to n appears exactly once i ...

  8. Codeforces Round 56-B. Letters Rearranging(思维)

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  9. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

随机推荐

  1. Android 属性动画监听事件与一个菜单的例子

    简单监听事件 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3 ...

  2. 「SCOI2015」小凸玩矩阵

    题目链接 问题分析 题目给了充足的暗示,我们只需要二分答案然后跑匈牙利即可.要相信匈牙利的速度 参考程序 #include <bits/stdc++.h> using namespace ...

  3. JS框架_(JQuery.js)带阴影贴纸标签按钮

    百度云盘 传送门 密码:azo6 纯CSS带阴影贴纸标签按钮效果: <!doctype html> <html> <head> <meta charset=& ...

  4. jmxtrans + influxdb + granafa 监控套件使用手册

    需求说明 随着大数据组件的日益完善,需要随时随地保持各个组件的日常运行,对各个组件的监控势在必行.为了减少运维部门的负担,通过筛选,我们使用 jmxtrans + influxdb + granafa ...

  5. 基于dubbo的分布式系统(一)安装docker

    1.安装过程参考: #uname -r  查看内核是否高于3.10 #sudo yum update root权限登陆确保yum包最新 #sudo yum remove docker docker-c ...

  6. 191022Django模板

    一.变量和句点符深度查找 字符串变量引用 def show_time(request): now_time = datetime.datetime.now() return render(reques ...

  7. spark MLlib 概念 2:Stratified sampling 层次抽样

    定义: In statistical surveys, when subpopulations within an overall population vary, it is advantageou ...

  8. Uep查询语句总结

    今天没事干总结一下uep查询语句: 第一种方法: 注意在实体写上对应的构造方法 package com.haiyisoft.entity.wz; import java.math.BigDecimal ...

  9. XPath2Doc,一个半自动采集网页生成Word Docx文件的工具,带企查查和天眼查模板

    原始出处:https://www.cnblogs.com/Charltsing/p/XPath2Doc.html 很多人需要从网站采集一些数据填写Word模板,手工操作费时费力还容易出错,所以我给朋友 ...

  10. 使用zipkin2在SpringCloud2.0环境下追踪服务调用情况

    1.目的: 使用zipkin2.0在Spring Cloud 2.0环境下,追踪服务调用情况. 2.所需组件: zipkin2.0,Spring Cloud 2.0,Eureka Server,Eur ...