Codeforces Round #324 (Div. 2) C. Marina and Vasya 贪心
C. Marina and Vasya
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/584/problem/CDescription
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 Input
abc
xyc
Sample Output
HINT
题意
给你两个字符串,要求你构造出第三个字符串,使得第三个字符串和第一个字符串和第二个字符串的不同个数,都是k个
题解:
难点就是重叠的时候
只要过了下面数据就差不多了吧
3 2
abc
def
这个只要交替染色就好了
代码:
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> #include <stdlib.h> #include <map> #include <functional> #include <queue> #define N 100000+100 #define pf(x) ((x)*(x)) #define D(x) (1/x) #define LL long long using namespace std; const double PI=3.141592653589793; char s1[N],s2[N],s3[N]; bool v[N]; int l,t; int cnt; bool s; void build() { cnt=0; if(cnt==t)return; for(int i=0;i<l;i++) if(s1[i]==s2[i]) { v[i]=true; s3[i]=s1[i]; cnt++; if(cnt==t) return; } s=true; for(int i=0;i<l;i++) if(s1[i]!=s2[i]) { v[i]=true; if(s) s3[i]=s1[i]; else s3[i]=s2[i]; s=!s; if(s) cnt++; if(cnt==t) return; } } int main() { cin>>l>>t; cin>>s1>>s2; // l=strlen(s1); t=l-t; build(); s3[l]='\0'; if(cnt<t) {cout<<"-1"<<endl;return 0;} // cout<<i<<endl; for(int i=0;i<l;i++) if(!v[i]) { for(int j='a';j<='z';j++) if(s1[i]!=j && s2[i]!=j) {s3[i]=j;break;} } cout<<s3<<endl; return 0; }