551B 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 substrings equal either to b or c as possible. Substring of string x is a string formed by consecutive segment of characters from x. Two substrings of string x overlap if there is position i in string x occupied by both of them.

GukiZ was disappointed because none of his students managed to solve the problem. Can you help them and find one of possible strings k?

Input

The first line contains string a, the second line contains string b, and the third line contains string c (1 ≤ |a|, |b|, |c| ≤ 105, where |s| denotes the length of string s).

All three strings consist only of lowercase English letters.

It is possible that b and c coincide.

Output

Find one of possible strings k, as described in the problem statement. If there are multiple possible answers, print any of them.


Sample test(s)

Input
aaa
a
b
Output
aaa
Input
pozdravstaklenidodiri
niste
dobri
Output
nisteaadddiiklooprrvz
Input
abbbaaccca
ab
aca
Output
ababacabcc

解题思路:一开始没看懂题意一直WA,原来发现就是一暴力枚举,首先枚举第2个串出现的次数,计算此时第3个串能够出现的次数,取所有情况两个串总共出现的次数的最大值,最后直接输出即可。

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <algorithm>
#include <functional>
using namespace std;
const int maxn = 100010;
int c1[30], c2[30], c3[30];
char s1[maxn], s2[maxn], s3[maxn];

int main() {

    //freopen("aa.in", "r", stdin);
    int x1 = 0x3f3f3f3f, x2;
    int max_num, x, y;
    scanf("%s", s1);
    scanf("%s", s2);
    scanf("%s", s3);
    int l1 = strlen(s1);
    int l2 = strlen(s2);
    int l3 = strlen(s3);
    memset(c1, 0, sizeof(c1));
    memset(c2, 0, sizeof(c2));
    memset(c3, 0, sizeof(c3));
    for(int i = 0; i < l1; ++i) {
        c1[s1[i]-'a']++;
    }
    for(int i = 0; i < l2; ++i) {
        c2[s2[i]-'a']++;
    }
    for(int i = 0; i < l3; ++i) {
        c3[s3[i]-'a']++;
    }
    for(int i = 0; i < 26; ++i) {
        if(c2[i] > 0) {
            x1 = min(x1, c1[i] / c2[i]);
        }
    }
    max_num = 0;
    x = 0;
    y = 0;
    for(int i = 0; i <= x1; ++i) {
        x2 = 0x3f3f3f3f;
        for(int j = 0; j < 26; ++j) {
            if(c3[j] > 0) {
                x2 = min(x2, (c1[j] - i * c2[j]) / c3[j]);
            }
        }
        if(i + x2 > max_num) {
            max_num = i + x2;
            x = i, y = x2;
        }
    }
    for(int i = 0; i < x; ++i) {
        printf("%s", s2);
    }
    for(int i = 0; i < y; ++i) {
        printf("%s", s3);
    }
    for(int i = 0; i < 26; ++i) {
        int t = c1[i] - x * c2[i] - y * c3[i];
        for(int j = 0; j < t; ++j) {
            printf("%c", 'a'+i);
        }
    }
    printf("\n");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值