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个构造剩下要求相同的,然后构造不同的
被这个水题的题意坑了。。。
代码:
给定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; }
相关文章推荐
- 1.一些 贪心算法 的简单思维题:
- 贪心算法——字典序最小问题
- 贪心算法——区间调度问题
- Jump Game I,II 贪心
- Wildcard Matching
- 贪心法实现无向图的划分 代码
- 贪心题目循环和控制台折行
- HDOJ 1009
- 【解题报告】【USACO】酸奶工厂
- 【解题报告】【】交谊舞
- POJ 1328
- Best Cow Line
- HDU 1009 Fatmouse's Trade
- POJ2377 Bad Cowtractors
- 贪心 hdu 1003
- 最大容器
- 满足和为定值的两个数或多个数
- Codeforces Round #300
- HUD1052 __ P2 1002 __ Tian Ji -- The Horse Racing
- 2015年国家集训队测试 BZOJ3816矩阵变幻