Codeforces 584 C Marina and Vasya【构造+贪心】

来源:互联网 发布:好莱坞爱情电影知乎 编辑:程序博客网 时间:2024/06/03 05:41

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 exactlyt characters. Help Vasya find at least one such string.

More formally, you are given two strings s1,s2 of lengthn and number t. Let's denote asf(a, b) the number of characters in which stringsa and b are different. Then your task will be to find any strings3 of lengthn, 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 andt (1 ≤ n ≤ 105,0 ≤ t ≤ n).

The second line contains string s1 of lengthn, consisting of lowercase English letters.

The third line contain string s2 of lengthn, consisting of lowercase English letters.

Output

Print a string of length n, differing from strings1 and froms2 in exactlyt characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.

Examples
Input
3 2abcxyc
Output
ayd
Input
1 0cb
Output
-1

题目大意:

给你n个长度的两个字符串 s1,s2,让你找到一个串,使得这个串和s1对应位子有t个字符不相同,和s2也有t个字符不相同(,输出任意一个解即可。


思路:


1、首先,使得有t个字符不相同,其实就是相当于让我们找一个串使得这个串对应s1和s2有n-t个字符相同。


2、那么我们分类讨论:

①如果s1和s2相同的字符(当然位子也要相同)的个数==n-t,那么我们输出的时候,对应相同的字符直接输出,否则输出一个字符和s1不同,和s2也不同(对应随便枚举三个字母就行,因为s1这个位子上有一个字母,s2这个位子上也有一个字母,只要我们枚举出三个字母就一定会有一个字母和这两个字母不同)。

②如果s1和s2相同的字符大于n-t,那么我们对应输出的时候,相同的字符输出n-t个即可,其余的输出与s1不同,与s2也不同的一个字符。

③如果s1和s2相同的字符小于n-t,那么对应输出的字符串还需要和s1相同的字符的个数为:yua=n-t-相同的数量,当然同理,还需要和s2相同的字符的个数也是:yub=n-t-相同的数量,那么不难理解,如果yua+yub>n-相同的个数,那么就要输出-1了,因为剩下的字符串的长度,不够满足题目要求了。

相反,如果yua+yub<=n-相同的个数,那么我们对应相同的字符直接输出,其余要输出yua个字符串a中的字符,yub个字符串b中的字符,再其余的,就输出一个字符和s1不同,和s2也不同的字符即可。


Ac代码:

#include<stdio.h>#include<string.h>using namespace std;char a[100050];char b[100050];char ans[100050];int vis[100050];int n,t;int main(){    while(~scanf("%d%d",&n,&t))    {        memset(vis,0,sizeof(vis));        scanf("%s%s",a,b);        int m=n-t;        int conts=0;        for(int i=0;i<n;i++)        {            if(a[i]==b[i])            {                vis[i]=1;                conts++;            }        }        if(conts==m)        {            for(int i=0;i<n;i++)            {                if(vis[i]==1)printf("%c",a[i]);                else                {                    if('x'!=a[i]&&'x'!=b[i])printf("x");                    else if('y'!=a[i]&&'y'!=b[i])printf("y");                    else if('z'!=a[i]&&'z'!=b[i])printf("z");                }            }            printf("\n");        }        else if(conts>=m)        {            int contss=0;            for(int i=0;i<n;i++)            {                if(vis[i]==1&&contss<m)printf("%c",a[i]),contss++;                else                {                    if('x'!=a[i]&&'x'!=b[i])printf("x");                    else if('y'!=a[i]&&'y'!=b[i])printf("y");                    else if('z'!=a[i]&&'z'!=b[i])printf("z");                }            }            printf("\n");        }        else        {            int yua=m-conts;            int yub=m-conts;            if(yua+yub>n-conts)            {                printf("-1\n");                continue;            }            for(int i=0;i<n;i++)            {                if(vis[i]==1)printf("%c",a[i]);                else                {                    if(yua>0)                    {                        printf("%c",a[i]);                        yua--;                    }                    else if(yub>0)                    {                        printf("%c",b[i]);                        yub--;                    }                    else                    {                        if('x'!=a[i]&&'x'!=b[i])printf("x");                        else if('y'!=a[i]&&'y'!=b[i])printf("y");                        else if('z'!=a[i]&&'z'!=b[i])printf("z");                    }                }            }            printf("\n");        }    }}



0 0