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 [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- Andorid获取状态栏高度
在应用开发中,有时我们需要用代码计算布局的高度,可能需要减去状态栏(status bar)的高度.状态栏高度定义在Android系统尺寸资源中status_bar_height,但这并不是公开可直接使 ...
- VMware与Centos系统
今日任务 1.Linux发行版的选择 2.vmware创建一个虚拟机(centos) 3.安装配置centos7 4.xshell配置连接虚拟机(centos) 选择性 pc可以选择 -纯系统 Lin ...
- http服务配置和apache
CentOS 6 httpd 程序环境 记录了httpd的主进程编号: 主程序文件: /usr/sbin/httpd /usr/sbin/httpd.worker /usr/sbin/http ...
- Oracle数据表之间的数据同步
保证两个数据表结构相同,如不相同只能同步相同字段; 只是思路,具体请根据需求修改. declare cursor csrn_mon is select * from table2; row_mon c ...
- kolla-ansible-----常用命令
常用命令 kolla-ansible prechecks -i multinode #部署前环境检测 kolla-genpwd #生成/etc/kolla/password.yml密码配置文件 kol ...
- Android 获取视频照片与刷新媒体库
1.获取本地所有视频 public void getLoadMedia() { Cursor cursor = UILApplication.instance.getApplicationContex ...
- ControlTemplate in WPF —— Menu
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...
- C# 实现播放RTSP 标准协议码流播放
http://www.codeproject.com/Articles/507218/Managed-Media-Aggregation-using-Rtsp-and-Rtphttp://www.st ...
- Unity3D 打包成Exe文件
Unity发布后一般都会一个exe文件和_data文件以及UnityPlayer.dll,如果把这三个文件整合成一个exe就可以(装逼)了 首先打开Winrar将这三个压缩: 压缩文件名设置为需要启动 ...
- navicat 系列软件一点击菜单栏就闪退
现象:安装多个版本都出现了闪退的现象 解决方案:后来发现,原来是启动了有道词典屏幕取词才会出现这种现象,关了有道就没事.