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 [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- 扫描工具-Nikto
靶机 192.168.1.101 攻击机 192.168.1.102 尝试ping命令 第一个工具 作用 Httrack --->(把目标的网站复制过来) 这样可以减少与目标系统的交互 mkdi ...
- war包部署到tomcat
1.maven web app打包成app.war.打包命令:mvn clean package Dmaven.test.skip=true war 是什么?里面有什么东西?a.web.app所有必 ...
- ubuntu16.04增大swap空间
参见->这里 参见->这里
- input(移动端iOS)输入内容时调用软件盘后页面底部留白问题
iOS/input输入框调用软键盘底部留白 只需input输入框失去焦点时,让页面自动下移即可恢复 <input placeholder="请输入用户名" v-model=& ...
- C#程序模拟登录批量获取各种邮件内容信息
一般来说,如果现实中你有这样一种需求“假如你是褥羊毛的羊毛党,你某日发现了一个app有一个活动,通过邮箱注册账号激活可以领5元红包,而恰恰你手上又有一批邮箱可用,那么批量获取邮箱中的激活链接去激活则是 ...
- Mysql和Orcale的区别
有很多应用项目, 刚起步的时候用MYSQL数据库基本上能实现各种功能需求,随着应用用户的增多,数据量的增加,MYSQL渐渐地出现不堪重负的情况:连接很慢甚至宕机,于是就有把数据从MYSQL迁到ORAC ...
- Viola-Jones(人脸检测)
Viola-Jones 人脸检测 1.Haar特征抽取 ‘ 2. Adaboost 算法
- java 设计模式 单例模式之饿汉模式/懒汉模式 singleton pattern
https://v.qq.com/x/page/e0364ung5zp.html 讲的不错, 关于 饿汉式单例模式 code Student 类: package com.test;//单例模式之 ...
- leetcode 1两数之和
使用哈希的方法:先将nums哈希表化,再遍历nums,寻找-nums[i]如果存在则为题目所求 class Solution { public: vector<int> twoSum(ve ...
- leetcode 148排序链表
优先队列容器,使用小顶堆排序:timeO(nlogn) spaceO(n) /** * Definition for singly-linked list. * struct ListNode { * ...