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], ...
随机推荐
- [Swift通天遁地]九、拔剑吧-(13)创建页面的景深视差滚动效果
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- GIT分支的一些开发心得
从本地的master分支创建新的分支 $ git checkout -b dev Switched to a new branch 'dev' 上面那条命令可以分为两步 $ git branch de ...
- Java高质量20问
问题一:在多线程环境中使用HashMap会有什么问题?在什么情况下使用get()方法会产生无限循环? HashMap本身没有什么问题,有没有问题取决于你是如何使用它的.比如,你在一个线程里初始化了一个 ...
- 《Typecript 入门教程》 3、接口
转载:<TypeScript 中文入门教程> 3.接口 介绍 TypeScript的核心原则之一是对值所具有的shape进行类型检查. 它有时被称做“鸭式辨型法”或“结构性子类型化”. 在 ...
- android 中的Context(一)
context的功能如此强大,它是activity的父类. public abstract class Context { ... public abstract Object getSystemSe ...
- android ListView,GridView 设置某一项显示位置
在项目中有时会用到,当使用 listview 想让它显示某一项,当它又不在前面的位置,可以 使用 //让某一项显示出来(否则可能不在当前) listview.setSelection(positio ...
- 318 Maximum Product of Word Lengths 最大单词长度乘积
给定一个字符串数组words,找到length(word[i]) * length(word[j])的最大值,并且两个单词不含公共的字母.你可以认为每个单词只包含小写字母.如果不存在这样的两个单词,返 ...
- fcc 响应式框架Bootstrap 练习2
text-primary 属性值使标题直接变成了红色,text-center使标题直接居中 <h2 class="text-primary text-center"> ...
- Ajax——jq中ajax的使用
格式化表单 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- JS——i++与++i
先赋值后自增: var i = 0; var n1 = i++; alert(i);//返回1 alert(n1);//返回0 先自增后赋值: var i = 0; var n1 = ++i; ale ...