cf C. Marina and Vasya (字串處理_模擬吧)
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly tcharacters. Help Vasya find at least one such string.
More formally, you are given two strings s1, s2 of
length n and number t.
Let's denote as f(a, b) the number of characters in which strings a
The first line contains two integers n and t (1 ≤ n ≤ 105, 0 ≤ t ≤ n).
The second line contains string s1 of length n, consisting of lowercase English letters.
The third line contain string s2 of length n, consisting of lowercase English letters.
OutputPrint a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
Sample test(s) input3 2 abc xyc
aydinput
1 0 c boutput
-1
昨天寫的時候。。。。。題意理解錯了!!! 白白浪費好幾分鐘,英語讀題能力要加強啊!!!
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+100;
char s1[maxn],s2[maxn],ans[maxn];
int book[maxn];
int main()
{
int n,t,i,j,x,y,tt,t1,t2;
x=y=0;
scanf("%d%d",&n,&t);
scanf("%s%s",s1,s2);
t=n-t;
for(i=0;i<n;i++) {
if(s1[i]==s2[i]) {
x++;
book[i]=1;
}
else y++;
}
if(t>x+y/2) {
printf("-1\n");
return 0;
}
if(x>=t) {
for(i=0;i<n;i++) {
if(book[i] && t) {
ans[i]=s1[i];
t--;
}
else {
for(j=0;j<3;j++) {
if(s1[i]!='a'+j && s2[i]!='a'+j) {
ans[i]='a'+j;
break;
}
}
}
}
}
else {
tt=(t-x)*2;
t1=t2=t-x;
for(i=0;i<n;i++) {
if(book[i]) ans[i]=s1[i];
else if(tt) {
tt--;
if(t1) {
ans[i]=s1[i];
t1--;
}
else {
ans[i]=s2[i];
t2--;
}
}
else {
for(j=0;j<3;j++) {
if(s1[i]!='a'+j && s2[i]!='a'+j) {
ans[i]='a'+j;
break;
}
}
}
}
}
for(i=0;i<n;i++) printf("%c",ans[i]);
printf("\n");
return 0;
}