Codeforces Round #324 (Div. 2)C. Marina and Vasya set

                                                      C. Marina and Vasya
 

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.

Sample test(s)
input
3 2
abc
xyc
output
ayd
input
1 0
c
b
output
-1

题意:给你两个长度一样n的字符串,让你再构造一个字符串是的它与任意这给出的俩个字符串不同字母个数为t
题解:我是set乱作的,代码能力差,求谅解
///1085422276
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define TS printf("111111
");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define inf 100000
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
//****************************************
#define maxn 100000+10

set<int >G;
set<int >::iterator it;
char a[maxn],b[maxn],c[maxn];
int main()
{

    int n=read();
    int t=read();
    scanf("%s%s",a,b);
    FOR(i,0,n-1)
     if(a[i]==b[i])
         G.insert(i);
         t=n-t;
    if(t>G.size())
    {
        int tmp=(n-G.size())/2;
        if(tmp<t-G.size()){cout<<-1<<endl;return 0;}
           for(it=G.begin();it!=G.end();it++)c[*it]=a[*it];
           t=t-G.size();
           int flag=1;int flag2=0;
           for(int i=0;i<n;i++)
        {
            if(G.count(i)==0&&flag&&t)
            {
                c[i]=a[i];
                flag=0;
                flag2=1;
            }
            else if(G.count(i)==0&&flag2&&t)
            {
                c[i]=b[i];
                t--;
                flag=1;
                flag2=0;
            }
            else if(G.count(i)==0){
                 for(int j=0;j<26;j++)
                    if('a'+j!=a[i]&&'a'+j!=b[i])
                {
                    c[i]='a'+j;break;
                }
            }
        }
    }
    else {
        for(it=G.begin();it!=G.end();it++)
        {
            if(t!=0)
            {  c[*it]=a[*it];
            t--;
            }else {
             for(int j=0;j<26;j++)
                    if('a'+j!=a[*it]&&'a'+j!=b[*it])
                {
                    c[*it]='a'+j;break;
                }}

        }
        for(int i=0;i<n;i++)
        {
            if(G.count(i)==0)
            {
                for(int j=0;j<26;j++)
                    if('a'+j!=a[i]&&'a'+j!=b[i])
                {
                    c[i]='a'+j;break;
                }
            }
        }

    } for(int i=0;i<n;i++)
  printf("%c",c[i]);
    return 0;
}
代码
原文地址:https://www.cnblogs.com/zxhl/p/4862307.html