您的位置:首页 > 其它

Codeforces Round #324 (Div. 2) C. Marina and Vasya(贪心)

2016-01-28 18:50 471 查看
题意:

给定2个长度为N≤105的字符串a,b,定义f(a,b)为a与b相同位置不同字符的个数

现要求构造一个同样长度的字符串c,使得f(c,a)=f(c,b)=t,t≤N

分析:

显然相同的不变,不然构造相同的需要2个字符,一个和a相同,一个和b相同

需要相同的个数same=n−t,假设已有相同的个数为cnt,如果(n−cnt)/2+cnt<same,显然不可构造

构造办法,先选相同的,然后每2个构造剩下要求相同的,然后构造不同的

被这个水题的题意坑了。。。

代码:

//
//  Created by TaoSama on 2016-01-28
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

int n, d;
char s
, t
, ans
;

int main() {
#ifdef LOCAL
freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
//  freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);

while(scanf("%d%d%s%s", &n, &d, s + 1, t + 1) == 4) {
memset(ans, 0, sizeof ans);
int cnt = 0;
for(int i = 1; i <= n; ++i) cnt += s[i] == t[i];
int same = n - d;
if((n - cnt) / 2 + cnt < same) {
puts("-1");
continue;
}

cnt = min(cnt, same);
for(int i = 1, j = 1; i <= n && j <= cnt; ++i)
if(s[i] == t[i]) ans[i] = s[i], ++j;
same -= cnt;
for(int i = 1, j = 1; i <= n && j <= same; ++i)
if(s[i] != t[i] && !ans[i]) ans[i] = s[i], ++j;
for(int i = 1, j = 1; i <= n && j <= same; ++i)
if(s[i] != t[i] && !ans[i]) ans[i] = t[i], ++j;
for(int i = 1; i <= n; ++i) {
if(ans[i]) continue;
for(int j = 'a'; j <= 'z'; ++j)
if(s[i] != j && t[i] != j) {ans[i] = j; break;}
}
puts(ans + 1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  贪心