zoukankan      html  css  js  c++  java
  • codeforces 584C Marina and Vasya

    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
     1 #include<cstdio>
     2 #include<vector>
     3 #include<cmath>
     4 #include<queue>
     5 #include<map>
     6 #include<cstring>
     7 #include<algorithm>
     8 using namespace std;
     9 typedef long long ll;
    10 typedef unsigned long long ull;
    11 const int maxn=100005;
    12 int main()
    13 {
    14     int t,n;
    15     char s1[maxn],s2[maxn],s3[maxn];
    16     scanf("%d%d",&n,&t);
    17     scanf("%s%s",s1,s2);
    18     t=n-t;
    19     int k1=0,k2=0;
    20     for(int i=0;i<n;i++)
    21     {
    22         if(s1[i]==s2[i]&&(k1<t&&k2<t))s3[i]=s1[i],k1++,k2++;
    23         else if(s1[i]!=s2[i]||k1==t)
    24             for(int j=0;j<26;j++)
    25             if(s1[i]!='a'+j&&s2[i]!='a'+j)
    26             {
    27                 s3[i]=(char)('a'+j);
    28                 break;
    29             }
    30     }
    31     if(k1==t)
    32     {
    33         puts(s3);
    34         return 0;
    35     }
    36     for(int i=0;i<n;i++)
    37     {
    38         if(s1[i]==s2[i])continue;
    39         if(k1<t)
    40             s3[i]=s1[i],k1++;
    41         else if(k2<t)
    42             s3[i]=s2[i],k2++;
    43         else
    44         for(int j=0;j<26;j++)
    45         if(s1[i]!='a'+j&&s2[i]!='a'+j)
    46         {
    47             s3[i]=(char)('a'+j);
    48             break;
    49         }
    50     }
    51     if(k1<t||k2<t)puts("-1");
    52     else puts(s3);
    53     return 0;
    54 }
    Output
    -1
  • 相关阅读:
    Java Volatile keyword
    解决 The &#39;InnoDB&#39; feature is disabled; you need MySQL built with &#39;InnoDB&#39; to have it working
    【玩转cocos2d-x之三十九】Cocos2d-x 3.0截屏功能集成
    【DP】UVA 624 CD 记录路径
    ns3加入模块之vanet-highway
    awk向脚本传递參数(二)
    【传递正能量】献给默默追梦的人
    算法(第四版)学习笔记之java实现可以动态调整数组大小的栈
    Webstorm/IntelliJ Idea 过期破解方法
    CenterOS下安装NodeJS
  • 原文地址:https://www.cnblogs.com/homura/p/4999324.html
Copyright © 2011-2022 走看看