Codeforces Round #324 (Div. 2) C. Marina and Vasya 字符串处理

C. Marina and Vasya
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

Sample test(s)
input
3 2
abc
xyc
output
ayd
input
1 0
c
b
output
-1
题意, 要求与两个字符串的不同字符都为t的第三个字符串。
先求出两个字符串的相同的数num.如果,n - t <= num,则只需要把这num个相同的字符选其中的一部分就可以了。否则,尽可能的一半与1字符串相同,别一半与2字符串相同。若仍然不够,则无解。

#define N 300005
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
int n,t,num;
char s1[N],s2[N],ans[N];
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
     while(S2(n,t)!=EOF)
    {
        SS(s1);
        SS(s2);
        num = 0;
        t = n - t;
        FI(n) ans[i] = s1[i];
        ans[n] = '\0';
        FI(n){
            if(s1[i] == s2[i])
            num++;
        }
        if(num >= t){
            FI(n){
                if(t > 0){
                    if(s1[i] == s2[i]){
                        ans[i] = s1[i];
                        t--;
                    }
                    else
                    {
                      FJ(26){
                           char c = 'a' + j;
                           if(s1[i] != c && s2[i] != c){
                               ans[i] = c;
                               break;
                           }
                       }
                    }
                }
                else {
                   FJ(26){
                       char c = 'a' + j;
                       if(s1[i] != c && s2[i] != c){
                           ans[i] = c;
                           break;
                       }
                   }
                }
            }
            printf("%s\n",ans);
        }
        else if((n - num) / 2 + num >= t){
            //printf("%d %d %d\n",n,num,t);
            int n1 = t - num;
            int n2 = n1;
            int first = 1;
            FI(n){
                if(s1[i] == s2[i]){
                    ans[i] = s1[i];
                }
                else {
                    if(first == 1){
                        ans[i] = s1[i];
                        n2--;
                        if(n2 == 0){
                            n2 = n1;
                            first++;
                        }
                    }
                    else if(first == 2){
                        ans[i] = s2[i];
                        n2--;
                        if(n2 == 0){
                            n2 = n1;
                            first++;
                        }
                    }
                    else {
                        FJ(26){
                           char c = 'a' + j;
                           if(s1[i] != c && s2[i] != c){
                               ans[i] = c;
                               break;
                           }
                       }
                    }
                }
            }
            printf("%s\n",ans);
        }
        else {
            printf("-1\n");
        }
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值