codeforces 324# C. Marina and Vasya (贪心)

题目:http://codeforces.com/contest/584/problem/C

题意:给定两个字符串s1和s2,求字符串s3使得s3与s1对应位置字符不同的个数为t,并且s3与s2对应位置字符不同的个数也为t。

C. Marina and Vasya
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 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 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 ≤ 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

分析:如果构造不出s3,就输出-1。由于找一个与另外两个字符的字符很好找,那么构造不出s3的情况是,s3和s1,s3和s2对应位置相同字符的个数不足。那么最大限度地利用一个位置的贡献就好了,怎么最大限度利用一个位置?当s1[i]==s2[i]的时候,把s3[i]赋成s1[i]。剩下的位置s1和s2交替贡献字符就行了。最后看满不满足条件。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
const int maxn = 1e6;
char s1[maxn],s2[maxn],ans[maxn];
int main()
{
	int n,t,i,j,lack;
	cin>>n>>t;
	cin>>s1>>s2;
	lack=n-t;
	int cnt1=0,cnt2=0;
	memset(ans,'\0',sizeof(ans));
	for(i=0;i<n;i++)
	{
		if(cnt1==lack)
			break;
		if(s1[i]==s2[i])
		{
			ans[i]=s1[i];
			s1[i]=s2[i]='#';
			cnt1++;
			cnt2++;
		}
	}
//	printf("%s %s %s\n",ans,s1,s2);
	int fg=1;
	for(i=0;i<n;i++) if(s1[i]!='#')
	{
		if(cnt1==lack && cnt2==lack)
			break;
		if(cnt1==cnt2)
		{
			ans[i]=s1[i];
			cnt1++;
		}
		else
		{
			ans[i]=s2[i];
			cnt2++;
		}
		s1[i]=s2[i]='#';
	}
//	printf("%s %s %s",ans,s1,s2);
	for(i=0;i<n;i++) if(s1[i]!='#')
	{
		for(char ch='a';ch<='z';ch++)
		{
			if(ch!=s1[i] && ch!=s2[i])
			{
				ans[i]=ch;
				break;
			}
		}
	}
	if(cnt1!=lack || cnt2!=lack)
	{
		printf("-1\n");
		return 0;
	}
	printf("%s\n",ans);
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值