CodeForces - 584C Marina and Vasya (模拟)找规律

本博客详细介绍了CodeForces平台上的问题584C,主题围绕Marina和Vasya在字符串操作上的独特挑战。通过解析输入数据、逻辑分析和算法实现,探讨了如何寻找至少一个字符串,使得该字符串与给定的两个字符串在特定数量的位置上不同。问题涉及字符串比较、字符替换和条件判断,旨在培养解决复杂字符串问题的能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CodeForces - 584C
Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

 Status

Description

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 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 Input

Input
3 2
abc
xyc
Output
ayd
Input
1 0
c
b
Output
-1

Source

//题意:
给出两个长度为n的字符串,先要求构造出一个长度也为n的串使其满足其与这两个串在相同位置不相同的字符数有m个,如果不存在这样的串则输出-1
//思路:先找出两个串相同的字母的个数k,那么不同的个数为mm=n-k;
通过模拟可以知道当2*m<mm时肯定输出-1;
当符合情况时可以分两种:
1、当m+k>=n时:先将两串相同位置的不同的的mm个字符变成不等于它们的字符,然后肯定要有(m-mm)个位置相同的相同字符变成与它们不同的字符,剩下的都是相同的就行了。
2、当mm<2*t时:那么相同位置的相同字符直接输出即可,相同位置的不同字符有(mm-m)个a串的字符,有(mm-m)个b串字符,其余的是与a,b串都不相同的字符。(不太理解的可以自己找例子模拟一下就懂了)
Hait:要注意新串的字符都是  ‘a’<=s<='z'的。(在这块WA了5次)。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define ull unsigned long long
#define long long
#define IN __in64
#define N 100010
#define M 1000000007 
using namespace std;
char s[N];
char a[N],b[N];
int main()
{
	int n,m,i,j,k,kk,mm,mk,la,lb,l;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(s,'\0',sizeof(s));
		memset(a,'\0',sizeof(a));
		memset(b,'\0',sizeof(b));
		scanf("%s%s",a,b);
		k=0;
		for(i=0;i<n;i++)
		{
			if(a[i]==b[i])
				k++;
		}
		mm=n-k;
		mk=mm/2;
		if(2*m<mm)
			printf("-1\n");
		else if(m+k>=n)
		{
			kk=m-mm;
			for(i=0;i<n;i++)
			{
				char d;
				if(a[i]==b[i])
				{
					if(kk)
					{
						for(d='a';d<='z';d++)
						{
							if(d!=a[i])
							{
								s[i]=d;
								break;
							}
						}
						kk--;
					}
					else
						s[i]=a[i];
				}
				else
				{
					for(d='a';d<='z';d++)
					{
						if(d!=a[i]&&d!=b[i])
						{
							s[i]=d;
							break;
						}
					}
				}
			}
			puts(s);
		}
		else
		{
			la=lb=mm-m;l=mm-la-lb;
			for(i=0;i<n;i++)
			{
				if(a[i]==b[i])
					s[i]=a[i];
				else
				{
					if(la)
					{
						s[i]=a[i];
						la--;
					}
					else if(lb)
					{
						s[i]=b[i];
						lb--;
					}
					else if(l)
					{
						char d;
						for(d='a';d<='z';d++)
						{
							if(d!=a[i]&&d!=b[i])
							{
								s[i]=d;
								break;
							}
						}
						l--;
					}
				}
			}
			puts(s);
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值