题目链接:Codeforces 584C 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.
Examples
input
3 2
abc
xyc
output
ayd
input
1 0
c
b
output
-1
题意:让你构造一个n长度的字符串c,使得它与a串、b串的不同字符个数严格为m。
思路:记cnt为a、b串不同字符个数。首先当cnt > m*2,一定无解。
一、cnt >= m,我们从cnt个里面各选cnt-m个填与a、b串一样的字符,其它的填与a、b串均不同的字符;
二、cnt < m,找m - cnt个字符相同的位置,修改下就好了。
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MOD = 1e4 + 7;
const int MAXN = 1e5 + 10;
void add(LL &x, LL y) { x += y; x %= MOD; }
char a[MAXN], b[MAXN];
char V(char x, char y) {
for(int i = 0; i < 26; i++) {
char z = 'a' + i;
if(z != x && z != y)
return z;
}
}
int main()
{
int n, m;
while(scanf("%d%d", &n, &m) != EOF) {
scanf("%s%s", a, b);
int cnt = 0;
for(int i = 0; i < n; i++) {
cnt += (a[i] != b[i]);
}
if(cnt > m * 2) {
printf("-1\n");
continue;
}
if(cnt >= m) {
int num = cnt - m; int use = 0;
for(int i = 0; i < n; i++) {
if(a[i] == b[i]) {
printf("%c", a[i]);
}
else {
++use;
if(use <= num) {
printf("%c", a[i]);
}
else if(use > num && use <= 2*num) {
printf("%c", b[i]);
}
else {
printf("%c", V(a[i], b[i]));
}
}
}
}
else {
int num = m - cnt; int use = 0;
for(int i = 0; i < n; i++) {
if(a[i] == b[i]) {
use++;
if(use <= num) {
printf("%c", V(a[i], a[i]));
}
else {
printf("%c", a[i]);
}
}
else {
printf("%c", V(a[i], b[i]));
}
}
}
printf("\n");
}
return 0;
}