Codeforces Round #324 (Div. 2)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 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

题意:f(a,b)表示字符串a和b的不同字符个数。求f(a,c)=f(b,c)=t的字符串C
分两种情况,我这定义的是f(a,b)表示字符串a和b的相同字符个数, f(a,b)>=n-t和其他
第一种情况一定可以,留下n-t个相同的字符,其他的使其与字符串a,b都不同
第二种情况一部分可以,留下f(a,b)个相同的字符,再让n-t-f(a,b)个字符与a相同但与b不同,让n-t-f(a,b)个字符与b相同但与a不同

#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
using namespace std;

const int N = 100001;

char str2[N];
char str1[N];
bool flag[N];
char str3[N];

char a[] = "abcdefg";

int f(int n, char str1[], char str2[])
{
    int i;
    int ans = 0;
    for(i = 0; i < n; i++)
    {
        if(str1[i] == str2[i])
        {
            flag[i] = 1;
            ans++;
        }
    }
    return ans;
}

int main(void)
{
    int n, t;
    while(~scanf("%d%d", &n, &t))
    {
        int i, j;
        memset(flag, 0, sizeof(flag));
        memset(str3, 0, sizeof(str3));
        getchar();
        scanf("%s%s", str1, str2);

        int tmp = f(n, str1, str2);
        if(tmp >= n-t)
        {
            int k;
            for(k = 0,i = 0; i < n; i++)
            {
                if(flag[i] && k < n-t)//tong
                {
                    k++;
                    str3[i] = str1[i];
                }
                else
                {
                    for(j = 0; j < 3; j++)
                    {
                        if(str1[i] != a[j] && str2[i] != a[j])
                        {
                            str3[i] = a[j];
                            break;
                        }
                    }
                }
            }
        }
        else
        {
            int k = 0;
            int j_1 = 0, k_1 = 0;
            for(j = 0,i = 0; i < n; i++)
            {
                if(flag[i])
                {
                    str3[i] = str1[i];
                    j++;
                    k++;
                }
                else if(j_1 < n-t-tmp && str1[i] != str2[i])
                {
                    str3[i] = str1[i];
                    j_1++;
                    j++;
                }
                else if(k_1 < n-t-tmp && str1[i] != str2[i])
                {
                    str3[i] = str2[i];
                    k_1++;
                    k++;
                }
                else
                {
                    for(int cnt = 0; cnt < 3; cnt++)
                    {
                        if(str1[i] != a[cnt] && str2[i] != a[cnt])
                        {
                            str3[i] = a[cnt];
                            break;
                        }
                    }
                }
            }
        }
        if(f(n, str3, str2) == f(n, str3, str2) && f(n, str3, str2) == n-t)
        {
            printf("%s\n", str3);
        }
        else
            printf("-1\n");
    }
    return 0;
}

      
      
     
     
    
    
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值