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], ...
随机推荐
- IOC框架---什么是IOC
1 IoC理论的背景 我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. ...
- nginx入门学习
1.yum解决编译nginx所需的依赖包,之后你的nginx就不会报错了 yum install gcc patch libffi-devel python-devel zlib-devel bzip ...
- 316 Remove Duplicate Letters 去除重复字母
给定一个仅包含小写字母的字符串,去除重复的字母使得所有字母出现且仅出现一次.你必须保证返回结果是所有可能结果中的以字典排序的最短结果.例如:给定 "bcabc"返回 "a ...
- 函数 out 传值 分割
public void Jia(int a ,int b) { a = a + b; Console.WriteLine(a); } public void Jia1(int a,out int b) ...
- .net framework 3.5 安装报错 0x800F0954问题
windows Server 2019 .net framework 3.5 安装报错 0x800F0954问题 .net framework 3.5的安装教程:但是安装出现0x800F0954这个错 ...
- Python初学1
windows版python下载: https://pan.baidu.com/s/1dsAPp0C9PJUF73kFDdAzXQ 安装时勾选pip和Add python.exe to Path. w ...
- html5——多媒体(一)
<audio> 1.autoplay 自动播放 2.controls 是否显不默认播放控件 3.loop 循环播放 4.preload 预加载 同时设置autoplay时些属性失效 5.由 ...
- Tcl之looping
1 While loop while test body The while command evaluates test as an expression. If test is true, the ...
- 4星|《超级技术:改变未来社会和商业的技术趋势》:AI对人友好吗
超级技术:改变未来社会和商业的技术趋势 多位专家或经济学人编辑关于未来的预测,梅琳达·盖茨写了其中一章.在同类书中属于水平比较高的,专家只写自己熟悉的领域,分析与预测有理有据而不仅仅是畅想性质. 以下 ...
- NSOperationQueue和GCD的区别,以及在什么场合下使用
1> GCD是纯C语言的API .NSOperationQueue是基于GCD的OC的封装. 2> GCD只支持FIFO队列,NSOperationQueue可以方便设置执行顺序,设置最大 ...