字符串处理/贪心 Codeforces Round #307 (Div. 2) B. ZgukistringZ

题目传送门

 1 /*
 2     题意:任意排列第一个字符串,使得有最多的不覆盖a/b字符串出现
 3     字符串处理/贪心:暴力找到最大能不覆盖的a字符串,然后在b字符串中动态得出最优解
 4     恶心死我了,我最初想输出最多的a,再最多的b,然而并不能保证是最多的:(
 5 */
 6 #include <cstdio>
 7 #include <cstring>
 8 #include <string>
 9 #include <iostream>
10 #include <algorithm>
11 #include <cmath>
12 using namespace std;
13
14 const int MAXN = 1e5 + 10;
15 const int INF = 0x3f3f3f3f;
16 char s[MAXN], a[MAXN], b[MAXN];
17 int cnt_s[30], cnt_a[30], cnt_b[30];
18 int len_s, len_a, len_b;
19
20 int main(void)        //Codeforces Round #307 (Div. 2) B. ZgukistringZ
21 {
22 //    freopen ("B.in", "r", stdin);
23
24     while (scanf ("%s", s) == 1)
25     {
26         scanf ("%s", a);    scanf ("%s", b);
27         len_s = strlen (s);    len_a = strlen (a);    len_b = strlen (b);
28
29         memset (cnt_s, 0, sizeof (cnt_s));
30         memset (cnt_a, 0, sizeof (cnt_a));
31         memset (cnt_b, 0, sizeof (cnt_b));
32
33         for (int i=0; i<len_s; ++i)    cnt_s[s[i]-‘a‘]++;
34         for (int i=0; i<len_a; ++i)    cnt_a[a[i]-‘a‘]++;
35         for (int i=0; i<len_b; ++i)    cnt_b[b[i]-‘a‘]++;
36
37         int ans_a = 0, ans_b = 0;    int tot = 100000;
38         for (int i=0; i<30; ++i)    {if (cnt_a[i])    tot = min (tot, cnt_s[i] / cnt_a[i]);}
39         for (int i=0; i<=tot; ++i)
40         {
41             int p = 100000;
42             for (int j=0; j<30; ++j)    {if (cnt_b[j])    p = min (p, (cnt_s[j]  - i * cnt_a[j]) / cnt_b[j]);}
43             if (i + p > ans_a + ans_b)    {ans_a = i;    ans_b = p;}
44         }
45
46         for (int i=1; i<=ans_a; ++i)    printf ("%s", a);
47         for (int j=1; j<=ans_b; ++j)    printf ("%s", b);
48         for (int i=0; i<30; ++i)
49         {
50             for (int j=1; j<=cnt_s[i]-ans_a*cnt_a[i]-ans_b*cnt_b[i]; ++j)
51                 printf ("%c", ‘a‘ + i);
52         }
53         puts ("");
54     }
55
56     return 0;
57 }
时间: 2024-09-30 18:14:52

字符串处理/贪心 Codeforces Round #307 (Div. 2) B. ZgukistringZ的相关文章

Codeforces Round #307 (Div. 2) B. ZgukistringZ

Professor GukiZ doesn't accept string as they are. He likes to swap some letters in string to obtain a new one. GukiZ has strings a, b, and c. He wants to obtain string k by swapping some letters in a, so that k should contain as many non-overlapping

贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

题目传送门 1 /* 2 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 3 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 #include <iostream> 10 using namespace std; 11 12 const int MAX

贪心 Codeforces Round #191 (Div. 2) A. Flipping Game

题目传送门 1 /* 2 贪心:暴力贪心水水 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 using namespace std; 8 9 const int MAXN = 1e2 + 10; 10 const int INF = 0x3f3f3f3f; 11 int a[MAXN]; 12 13 int main(void) //Codeforces Round #191

贪心 Codeforces Round #301 (Div. 2) B. School Marks

题目传送门 1 /* 2 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 3 num1是输出的1的个数,numy是除此之外的数都为y,此时的numy是最少需要的,这样才可能中位数大于等于y 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 using na

贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

题目传送门 1 /* 2 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 3 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 typedef long long ll; 12 13 co

贪心 Codeforces Round #135 (Div. 2) C. Color Stripe

题目传送门 1 /* 2 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 3 同时不和下一个相等就是最优的解法 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 using namespace std; 9 10 const int MAXN = 5e5 + 10; 11 const int IN

找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones

题目传送门 1 /* 2 找规律/贪心:ans = n - 01匹配的总数,水 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 2e5 + 10; 12 const int INF =

贪心 Codeforces Round #263 (Div. 2) C. Appleman and Toastman

题目传送门 1 /* 2 贪心:每次把一个丢掉,选择最小的.累加求和,重复n-1次 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-1 13:20:01 7 File Name :A.cpp 8 *************************************************/ 9 10 #include <cstdio>

贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges

题目传送门 1 /* 2 题意:问最少增加多少值使变成递增序列 3 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 using namespace std; 9 10 typedef long long ll; 11 12 const int MAXN = 3e3 + 10;