字符串处理/贪心 Codeforces Round #307 (Div. 2) B. ZgukistringZ
2015-06-13 14:23
441 查看
题目传送门
/* 题意:任意排列第一个字符串,使得有最多的不覆盖a/b字符串出现 字符串处理/贪心:暴力找到最大能不覆盖的a字符串,然后在b字符串中动态得出最优解 恶心死我了,我最初想输出最多的a,再最多的b,然而并不能保证是最多的:( */ #include <cstdio> #include <cstring> #include <string> #include <iostream> #include <algorithm> #include <cmath> using namespace std; const int MAXN = 1e5 + 10; const int INF = 0x3f3f3f3f; char s[MAXN], a[MAXN], b[MAXN]; int cnt_s[30], cnt_a[30], cnt_b[30]; int len_s, len_a, len_b; int main(void) //Codeforces Round #307 (Div. 2) B. ZgukistringZ { // freopen ("B.in", "r", stdin); while (scanf ("%s", s) == 1) { scanf ("%s", a); scanf ("%s", b); len_s = strlen (s); len_a = strlen (a); len_b = strlen (b); memset (cnt_s, 0, sizeof (cnt_s)); memset (cnt_a, 0, sizeof (cnt_a)); memset (cnt_b, 0, sizeof (cnt_b)); for (int i=0; i<len_s; ++i) cnt_s[s[i]-'a']++; for (int i=0; i<len_a; ++i) cnt_a[a[i]-'a']++; for (int i=0; i<len_b; ++i) cnt_b[b[i]-'a']++; int ans_a = 0, ans_b = 0; int tot = 100000; for (int i=0; i<30; ++i) {if (cnt_a[i]) tot = min (tot, cnt_s[i] / cnt_a[i]);} for (int i=0; i<=tot; ++i) { int p = 100000; for (int j=0; j<30; ++j) {if (cnt_b[j]) p = min (p, (cnt_s[j] - i * cnt_a[j]) / cnt_b[j]);} if (i + p > ans_a + ans_b) {ans_a = i; ans_b = p;} } for (int i=1; i<=ans_a; ++i) printf ("%s", a); for (int j=1; j<=ans_b; ++j) printf ("%s", b); for (int i=0; i<30; ++i) { for (int j=1; j<=cnt_s[i]-ans_a*cnt_a[i]-ans_b*cnt_b[i]; ++j) printf ("%c", 'a' + i); } puts (""); } return 0; }
相关文章推荐
- HTML表单的提交
- 搭建hadoop集群环境
- 水题 Codeforces Round #307 (Div. 2) A. GukiZ and Contest
- 经典shell面试题整理
- Apache Benchmark修改,支持发送不同数据包
- Javascript中的方法和匿名方法实例详解
- UITextField知识点总结(一)
- RSA 公钥密码实验2
- 编译Android2.3.5源码,提供源码
- Scala Hello 示例
- shell中常用的特殊符号
- FaceBook/infer-分析C文件
- Sublime Text 2 – SFTP/FTP 安装与使用
- Android的WebView控件
- Linux && vim 批量替换
- [LeetCode] Valid Parentheses
- Android程序的入口点及深入生命周期及fragment的使用及viewpager
- Java实现对称式和非对称式加密
- bzoj1594
- HTML5 在canvas绘制一个矩形