CF #324 DIV2 C题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int MAX=1e5+7; char s1[MAX],s2[MAX],s3[MAX]; char getdif(char a,char b){
for(int i=0;i<26;i++)
if('a'+i!=a&&'a'+i!=b) return 'a'+i;
} ///考虑的方式时,假设当前字符串与A字符串相同,则不同的贡献值为(0,1),如果与B相同,则贡献值为(1,0),否则为(1,1) int main(){
int n,t,cnt;
while(scanf("%d%d",&n,&t)!=EOF){
scanf("%s%s",s1+1,s2+1);
cnt=0;
for(int i=1;i<=n;i++)
if(s1[i]!=s2[i]) cnt++;
if(t>n||cnt>2*t){ ///可以发现,这两种情况是无解的
puts("-1");
continue;
}
int a,b;
if(cnt<=t) a=b=cnt;///当不同的个数少于等于要求的个数时,直接让a=b=cnt;
else
a=b=cnt/2+cnt%2;////如果在于,则可以把cnt先平分,剩下看奇偶
int lcnt=t-a;////看还差多少个可以满足,这时考虑(1,1)的方法
if(n-cnt<lcnt){///如果相同的个数<差的个数
lcnt+=lcnt-(n-cnt);///则需要从不同的个数里把原来只考虑(1,0)或(0,1)的情况改成(1,1)
cnt-=lcnt-(n-cnt);///从原来减去
a=b=cnt/2+cnt%2;///更改a,b
}
for(int i=1;i<=n;i++){
if(s1[i]==s2[i]){
if(lcnt) s3[i]=getdif(s1[i],s2[i]),lcnt--;
else s3[i]=s1[i];
}
}
for(int i=1;i<=n;i++){
if(s1[i]!=s2[i]){
if(lcnt){
s3[i]=getdif(s1[i],s2[i]),lcnt--;
cnt--;
continue;
}
if(cnt==a&&a==b) s3[i]=getdif(s1[i],s2[i]), cnt--,a--,b--;
else{
if(a>b) s3[i]=s1[i],a--; else s3[i]=s2[i],b--;
cnt--;
}
}
}
for(int i=1;i<=n;i++)
printf("%c",s3[i]);
printf("\n");
}
return 0;
}
CF #324 DIV2 C题的更多相关文章
- CF #324 DIV2 E题
这题很简单,把目标位置排序,把目标位置在当前位置前面的往前交换,每次都是贪心选择第一个满足这样要求的数字. #include <iostream> #include <cstdio& ...
- CF #323 DIV2 D题
可以知道,当T较大时,对于LIS,肯定会有很长的一部分是重复的,而这重复的部分,只能是一个block中出现次数最多的数字组成一序列.所以,对于T>1000时,可以直接求出LIS,剩下T-=100 ...
- CF #316 DIV2 D题
D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- cf 442 div2 F. Ann and Books(莫队算法)
cf 442 div2 F. Ann and Books(莫队算法) 题意: \(给出n和k,和a_i,sum_i表示前i个数的和,有q个查询[l,r]\) 每次查询区间\([l,r]内有多少对(i, ...
- CF#345 div2 A\B\C题
A题: 贪心水题,注意1,1这组数据,坑了不少人 #include <iostream> #include <cstring> using namespace std; int ...
- codeforces round 422 div2 补题 CF 822 A-F
A I'm bored with life 水题 #include<bits/stdc++.h> using namespace std; typedef long long int LL ...
- codeforces round 421 div2 补题 CF 820 A-E
A Mister B and Book Reading O(n)暴力即可 #include<bits/stdc++.h> using namespace std; typedef lon ...
- Codeforces round 419 div2 补题 CF 816 A-E
A Karen and Morning 水题 注意进位即可 #include<bits/stdc++.h> using namespace std; typedef long long i ...
- codeforces round 418 div2 补题 CF 814 A-E
A An abandoned sentiment from past 水题 #include<bits/stdc++.h> using namespace std; int a[300], ...
随机推荐
- redis取经之路
redis基本数据结构 Redis使用的是自己构建的简单动态字符串(SDS)[simple dynamic string,SDS]的抽象类型,并将SDS用做Rdis的默认字符串表示 redis> ...
- python自动化测试学习笔记-4常用模块
常用模块 1.os 2.sys 3.random 4.string 5.time 6.hashlib 一.os模块 os模块主要用来操作文件.目录,与操作系统无关.要使用os模块首先要导入OS模块,用 ...
- JS压缩图片(canvas),返回base64码
上传图片时总会遇到图片过大上传不上去的问题,本方法是在网上搜的压缩图片的例子,我测试过了,确实能用,但是照搬别人的代码,发现压缩后图片会失真,不清晰,现经修改图片清晰度还可以,不仔细看差别不大,so, ...
- JS高级——apply与call
上下文调用模式 可以修改this的值,也就是可以修改函数的调用方式,apply.call可以修改函数调用上下文,也就是this的值 <script> var name = "莱昂 ...
- (转)Java任务调度框架Quartz入门教程指南(四)Quartz任务调度框架之触发器精讲SimpleTrigger和CronTrigger、最详细的Cron表达式范例
http://blog.csdn.net/zixiao217/article/details/53075009 Quartz的主要接口类是Schedule.Job.Trigger,而触发器Trigge ...
- ubuntu下查看如何配置pycharm
ubuntu中PyCharm的安装与卸载 https://blog.csdn.net/weixin_31484477/article/details/81133590 pycharm ModuleNo ...
- 00 python基础知识
''' ''' print('hello world!') ''' 变量 ''' # 变量的:‘tank’,会在内存中产生一份内存地址 #变量名:相当于一个门牌号,用于与变量进行绑定 # = :用来把 ...
- UpLoadify在IE下兼容问题
一.在IE9.IE10不能点击的问题解决 解决方法:进入uploadify的js文件中,搜索SWFUpload.prototype.getFlashHTML,找到它对应的语句,将方法全部替换为以下内容 ...
- Ansible实现zabbix服务器agent端批量部署
项目需求:由于搭建zabbix,需要每台服务器都需要安装监控端(agent)正常的的操作是一台一台去安装,这样确实有点浪费时间,这里为大家准备了一款开源 的自动化运维工具Ansible,相信大家也很熟 ...
- js兼用性
1.document.formName.item("itemName") 问题 说明:IE下,可以使用document.formName.item("itemName&q ...