【打CF,学算法——二星级】Codeforces 584C Marina and Vasya (简单构造)

【CF简介】

提交链接:http://codeforces.com/problemset/problem/584/C


题面:

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

题目大意:

    构造一任意串,使得其与给定两串刚好有t个字符不同。若无法构造输出-1。


解题:

    分类讨论。

    统计给定两串的不同字符数为x。

    1.t=x

    保持原来相同部分不变,不同部分与两串都不相同。

    2.t>x

    不同部分,产生一个字符,使之不同,相同部分取(t-x)个使之不同。

    3.t<x

    保持相同部分不变,另挑(x-t)个原不同位置,使之与a串相同,同理,b串。

    此操作最后需检验一遍,可能无法构造。输出-1。


代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <map>
#include <vector>
#define LL long long
using namespace std;
bool status[100010];
char generate(char a,char b)
{
	char tmp;
	for(int i=0;i<26;i++)
	{
		tmp='a'+i;
		if(tmp!=a&&tmp!=b)
			return tmp;
	}
}
int count_dif(string a,string b,int len)
{
	int res=0;
	for(int i=0;i<len;i++)
	  if(a[i]!=b[i])
		  res++;
    return res;
}
int main()
{
    int n,t,dif=0;
	string a,b,c;
	cin>>n>>t;
    cin>>a>>b;
	c=string(n,'a');
	vector <int> pos;
	memset(status,0,sizeof(status));
    for(int i=0;i<n;i++)
	{
		if(a[i]!=b[i])
		  dif++;
		else
		  status[i]=1;
	}
	if(t==dif)
	{
		for(int i=0;i<n;i++)
		{
			if(status[i])
				c[i]=a[i];
			else
				c[i]=generate(a[i],b[i]);
		}
	}
	else if(t>dif)
	{
		int cnt=0;
		for(int i=0;i<n;i++)
		{
			if(status[i])
			{
				if(cnt<(t-dif))
				{
					c[i]=generate(a[i],b[i]);
					cnt++;
				}
				else
					c[i]=a[i];
			}
			else
			{
				c[i]=generate(a[i],b[i]);
			}
		}
	}
	else
	{
		int cnt1=0,cnt2=0;
		for(int i=0;i<n;i++)
		{
           if(status[i])
			   c[i]=a[i];
		   else
		   {
			   if(cnt1<(dif-t))
			   {
				   c[i]=a[i];
				   cnt1++;
			   }
			   else if(cnt2<(dif-t))
			   {
				   c[i]=b[i];
				   cnt2++;
			   }
			   else
				   c[i]=generate(a[i],b[i]);
		   }
		}
	}
	if(count_dif(a,c,n)==t&&count_dif(b,c,n)==t)
		cout<<c<<endl;
	else
		cout<<"-1\n";
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值