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

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 s1, s2 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 ≤ 105, 0 ≤ 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.

Examples
Input
3 2
abc
xyc
Output
ayd
Input
1 0
c
b
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");
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值