C. Marina and Vasya
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. 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 and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print - 1.
Input
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.
Output
Print 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)
input
3 2
abc
xyc
output
ayd
input
1 0
c
b
output
-1
题意, 要求与两个字符串的不同字符都为t的第三个字符串。
先求出两个字符串的相同的数num.如果,n - t <= num,则只需要把这num个相同的字符选其中的一部分就可以了。否则,尽可能的一半与1字符串相同,别一半与2字符串相同。若仍然不够,则无解。
#define N 300005
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
int n,t,num;
char s1[N],s2[N],ans[N];
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while(S2(n,t)!=EOF)
{
SS(s1);
SS(s2);
num = 0;
t = n - t;
FI(n) ans[i] = s1[i];
ans[n] = '\0';
FI(n){
if(s1[i] == s2[i])
num++;
}
if(num >= t){
FI(n){
if(t > 0){
if(s1[i] == s2[i]){
ans[i] = s1[i];
t--;
}
else
{
FJ(26){
char c = 'a' + j;
if(s1[i] != c && s2[i] != c){
ans[i] = c;
break;
}
}
}
}
else {
FJ(26){
char c = 'a' + j;
if(s1[i] != c && s2[i] != c){
ans[i] = c;
break;
}
}
}
}
printf("%s\n",ans);
}
else if((n - num) / 2 + num >= t){
//printf("%d %d %d\n",n,num,t);
int n1 = t - num;
int n2 = n1;
int first = 1;
FI(n){
if(s1[i] == s2[i]){
ans[i] = s1[i];
}
else {
if(first == 1){
ans[i] = s1[i];
n2--;
if(n2 == 0){
n2 = n1;
first++;
}
}
else if(first == 2){
ans[i] = s2[i];
n2--;
if(n2 == 0){
n2 = n1;
first++;
}
}
else {
FJ(26){
char c = 'a' + j;
if(s1[i] != c && s2[i] != c){
ans[i] = c;
break;
}
}
}
}
}
printf("%s\n",ans);
}
else {
printf("-1\n");
}
}
//fclose(stdin);
//fclose(stdout);
return 0;
}