CodeForces #307 Div.2 B. ZgukistringZ

来源:互联网 发布:我的世界linux启动器 编辑:程序博客网 时间:2024/05/19 04:55

B. ZgukistringZ

题目连接:http://codeforces.com/problemset/problem/551/B

题意:给出3个字符串a,b,c,要求改变字符串a中的字符顺序,使得可以出现的字符串b和字符串c出现的次数最多.

题解:先匹配字符串b,枚举字符串a中出现字符串b的最多次数,然后从剩余的字符串中匹配字符串c,最后按字母序输出剩下的串。

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int counta[30], countb[30], countc[30];const int MAX = 1e5+5;int main() {    char a[MAX],b[MAX],c[MAX];    while(gets(a)) {        gets(b);        gets(c);        int lena = strlen(a);        int lenb = strlen(b);        int lenc = strlen(c);        memset(counta, 0, sizeof(counta));        memset(countb, 0, sizeof(countb));        memset(countc, 0, sizeof(countc));        for(int i = 0; i < lena; i++)            counta[ a[i] - 'a' ] ++;        for(int i = 0; i < lenb; i++)            countb[ b[i] - 'a' ] ++;        for(int i = 0; i < lenc; i++)            countc[ c[i] - 'a' ] ++;        int ans = 1e8;        for(int i = 0; i < 26; i++)            if(countb[i])                 ans = min(ans, counta[i] / countb[i]);        int ansb = 0, ansc = 0;        for(int i = 0; i <= ans; i++) {            int cnt = 1e8;            for(int j = 0; j < 26; j++)                if(countc[j])                     cnt = min(cnt, (counta[j] - countb[j] * i) / countc[j]);            if(i + cnt > ansb + ansc) {                ansb = i;                ansc = cnt;            }        }        for(int i = 0; i < ansb; i++)             printf("%s",b);        for(int i = 0; i < ansc; i++)             printf("%s",c);        for(int i = 0; i < 26; i++)            for(int j = 0; j < counta[i] - countb[i] * ansb - countc[i] * ansc; j++) {                printf("%c", i + 'a');            }        printf("\n");    }    return 0;}
0 0
原创粉丝点击