Acm Marina and Vasya的题解

本文介绍了一个字符串匹配的问题,任务是找出两个长度相同的字符串中,存在第三个字符串,在恰好t个字符上与前两者不同。通过分析给出的字符串,文章提供了一种算法实现思路及代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Examples

input

3 2

abc

xyc

output

ayd

input

1 0

c

b

output

-1

http://codeforces.com/problemset/problem/584/C

题意不难理解

也没有什么坑点   就是容易搞错   求得不同的位置字母    转换以下   就成了求n-t个相同的位置字母了  首先求出两行字母串本来就相同的位置字母的数量   根据这个和要求分为三种情况(其实我不知道是不是可以少分点   当时就是这么想的   我也难得去想怎么简化)   下面代码都有哈

 

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

const int MAXN=100010;
char ans[MAXN],bns[MAXN];

int main(){
    
    int num,count;
    int n,t,k1,k2;
    scanf("%d%d",&n,&t);
    scanf("%s%s",ans,bns);
    num=0;
    count=n-t;
    for(int i=0;i<n;i++){
        
        if(ans[i]==bns[i]) num++;
        
    }
    if(num==count){
        
        for(int i=0;i<n;i++){
            
            if(ans[i]==bns[i]) printf("%c",ans[i]);
            else{
                
                if(ans[i]!='a'&&bns[i]!='a') printf("a");
                else if(ans[i]!='b'&&bns[i]!='b') printf("b");
                else if(ans[i]!='c'&&bns[i]!='c') printf("c");
                
            }
            
        }
        printf("\n");
        
    }
    else if(num<count){
        
        if(count-num>(n-num)/2) printf("-1\n");
        else{
            
            k1=k2=0;
            for(int i=0;i<n;i++){
                
                if(ans[i]==bns[i]) printf("%c",ans[i]);
                else{
                    
                    if(k1!=count-num){
                        
                        printf("%c",ans[i]);
                        k1++;
                        
                    }
                    else{
                        
                        if(k2!=count-num){
                            
                            printf("%c",bns[i]);
                            k2++;
                            
                        }
                        else{
                            
                            if(ans[i]!='a'&&bns[i]!='a') printf("a");
                            else if(ans[i]!='b'&&bns[i]!='b') printf("b");
                            else if(ans[i]!='c'&&bns[i]!='c') printf("c");
                            
                        }
                        
                    }
                
                }
                
            }
            printf("\n");
            
        }
        
    }
    else if(num>count){
        
        k1=0;
        for(int i=0;i<n;i++){
            
            
            if(ans[i]==bns[i]){
                
                if(k1!=num-count){
                    
                    if(ans[i]!='a'&&bns[i]!='a') printf("a");
                    else if(ans[i]!='b'&&bns[i]!='b') printf("b");
                    else if(ans[i]!='c'&&bns[i]!='c') printf("c");
                    k1++;
                    
                }
                else{
                    
                    printf("%c",ans[i]);
                    
                }
                
            }
            else{
                
                if(ans[i]!='a'&&bns[i]!='a') printf("a");
                else if(ans[i]!='b'&&bns[i]!='b') printf("b");
                else if(ans[i]!='c'&&bns[i]!='c') printf("c");
                
            }
            
        }
        printf("\n");
        
    }
    
}

 

转载于:https://www.cnblogs.com/irrelevant/p/5514483.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值