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

来源:互联网 发布:阿铭linux 编辑:程序博客网 时间:2024/05/22 12:22
CodeForces - 584C
Marina and Vasya
Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

Submit 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 2abcxyc
Output
ayd
Input
1 0cb
Output
-1

Source

Codeforces Round #324 (Div. 2)
//题意:
给出两个长度为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--;}elses[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;}


0 0
原创粉丝点击