1. 程式人生 > >cf C. Marina and Vasya (字串處理_模擬吧)

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 s1s2 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 thatf(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 ≤ 1050 ≤ 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

昨天寫的時候。。。。。題意理解錯了!!! 白白浪費好幾分鐘,英語讀題能力要加強啊!!!

#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;
}