您的位置:首页 > 其它

字符串处理/贪心 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: