CodeForces 584C - Marina and Vasya(构造)

本文介绍了一道编程题的解决思路及代码实现。题目要求构造一个特殊字符串,使其与两个给定字符串在指定数量的位置上存在差异。文章详细解析了解题过程,并提供了一段完整的AC代码。

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

题意:
对于长度为n的两个字符串,要构造一个新的字符串,使得他与任意的字符串都有对应位置的t个不同字符.

解题思路:
对于这个字符串,必然和原字符串有n-t个相同.
那么在找相同的时候,选择a和b相同的是最优解.
记录a和b相同的个数为sum.
那么对于a和新字符串,他们最多的相同个数为sum + (n-sum)/2 (因为也需要和b有(n-sum)/2相同).
所以最大相同个数如果小于n-t,必然是不成立的.

在优先填充完sum部分后,再分别对于a和b进行单独相同填充.
最后的剩余部分要使得与a和b都不相同.

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
char a[maxn];
char b[maxn];
char c[maxn];
int main()
{
    int n,t;
    cin>>n>>t;
    cin>>a>>b;
    int sum = 0;
    for(int i = 0;a[i];i++) if(a[i] == b[i])    sum++;
    int m = n-t;
    if(sum + (n-sum)/2 < m)
    {
        printf("-1");
        return 0;
    }
    int k = min(sum,m);
    int cnt = 0;
    for(int i = 0,cnt = 1;a[i]&&cnt <= k;i++)   if(a[i] == b[i])    c[i] = a[i],cnt++;
    int s = m-k;
    for(int i = 0,cnt = 1;a[i] && cnt <= s;i++) if((a[i] != b[i]) && !c[i]) c[i] = a[i],cnt++;
    for(int i = 0,cnt = 1;a[i] && cnt <= s;i++)  if((a[i] != b[i]) && !c[i]) c[i] = b[i],cnt++;
    for(int i = 0;a[i];i++)
    {
        if(!c[i])
        {
            for(char j = 'a';j <= 'z';j++)
                if(j != a[i] && j != b[i])  {c[i] = j;break;}
        }
    }
    cout<<c;
    return 0;
}
每年秋天,叶卡捷琳堡最受欢迎的电影院之一的所有电影都会座无虚席,因为学生们喜欢坐在舒适的椅子上看电影,而不是参加讲座和研讨会。不幸的是,电影院里的两排座位之间的距离很小,人们在电影放映前就座时经常被别人绊倒。进入大厅后,访客可以选择从排的哪一端(左端或右端)就座。他选择的方式是让他绊倒的人数最少。如果左右两端的数字相等,则访客会选择最靠近他座位的那排末端。 哲学系学生瓦夏 (Vasya) 是一名狂热的电影爱好者,同时也是一名数学狂热爱好者。他是第一个购买新电影首映门票的人。当瓦夏走进大厅,在自己的座位上坐下时,他看到这一排的其他座位还空着。瓦夏知道,当展览开始时,大厅里已经挤满了人。因此,不少其他观众在入座时都会被他的脚绊倒。尽管瓦夏讨厌数学,但他还是能够立即估计出在展览前被他绊倒的最大人数。你能做同样的事吗? 输入 Input The only input line contains the total number of seats n in the row where Vasya is sitting and the number of his seat k (1 ≤ k ≤ n ≤ 50; n is even). These integers are separated with a space. The seats in the row are numbered starting with one. 唯一的输入行包含Vasya 所在行的 座位总数 n以及他的座位号k (1 ≤ k ≤ n ≤ 50; n 为偶数)。这些整数之间用空格分隔。一排的座位是从一开始编号的。 输出 Output Output the maximal number of people who would stumble over Vasya's feet. 输出最多有多少人会被 Vasya 绊倒。
最新发布
09-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值