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 ab, 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
Note

In the third sample, this optimal solutions has three non-overlaping substrings equal to either b or c on positions 1 – 2 (ab), 3 – 4 (ab),5 – 7 (aca). In this sample, there exist many other optimal solutions, one of them would be acaababbcc.

一个字符串,字母可以两两交换,问交换后出现b,c字符串次数最多的字符串。对字母标记数组,然后找出全是b串能出现最大次数pre1,然后从0到pre1遍历,每次能找出c的出现次数,再找其中的最大值并将b,c的次数记录下来,最后按照次数输出b,c,再把剩下的字母输出就好了,因为是任意的交换,其实就是重新排列。

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
using namespace std;
int leap[4][30], pre[30];
int min(int a, int b)
{
    return a < b ? a : b;
}
int max(int a, int b)
{
    return a>b ? a : b;
}
int find2()
{
    int i, temp = 0, leap1 = 0;
    if (leap[2][1] != 0)
    {
        temp = leap[1][1] / leap[2][1];
        leap1 = 1;
    }
    for (i = 2; i <= 26; i++)
    {
        if (leap[2][i] == 0)continue;
        else if (!leap1)
        {
            temp = leap[1][i] / leap[2][i];
            leap1 = 1;
        }
        else
            temp = min(temp, leap[1][i] / leap[2][i]);
    }
    return temp;
}
int find3()
{
    int i, temp = 0, leap1 = 0;
    if (leap[3][1] != 0)
    {
        temp = leap[1][1] / leap[3][1];
        leap1 = 1;
    }
    for (i = 2; i <= 26; i++)
    {
        if (leap[3][i] == 0)continue;
        else if (!leap1)
        {
            temp = leap[1][i] / leap[3][i];
            leap1 = 1;
        }
        else
            temp = min(temp, leap[1][i] / leap[3][i]);
    }
    return temp;
}
int find4()
{
    int i, temp = 0, leap1 = 0;
    if (leap[3][1] != 0)
    {
        temp = pre[1] / leap[3][1];
        leap1 = 1;
    }
    for (i = 2; i <= 26; i++)
    {
        if (leap[3][i] == 0)continue;
        else if (!leap1)
        {
            temp = pre[i] / leap[3][i];
            leap1 = 1;
        }
        else
            temp = min(temp, pre[i] / leap[3][i]);
    }
    return temp;
}
int main()
{
    int i, j, m, n, ans, len1, len2, len3, pre1, pre2, Ans, temp1, temp2;
    char a[100005], b[100005], c[100005];
    for (i = 0; i <= 3;i++)
    for (j = 0; j <= 29; j++)
        leap[i][j] = 0;
    scanf("%s", a); getchar();
    scanf("%s", b); getchar();
    scanf("%s", c);
    len1 = strlen(a); len2 = strlen(b); len3 = strlen(c);
    for (i = 0; i < len1; i++)
    {
        leap[1][a[i] - 'a'+1]++;
    }
    for (i = 0; i < len2; i++)
    {
        leap[2][b[i] - 'a'+1]++;
    }
    for (i = 0; i < len3; i++)
    {
        leap[3][c[i] - 'a'+1]++;
    }
    pre1 = find2();
    pre2 = find3();
    Ans = 0; temp1 = temp2 = 0;
    for (i = 0; i <= pre1; i++)
    {
        for (j = 1; j <= 26; j++)
        {
            pre[j] = leap[1][j] - i*leap[2][j];
        }
        ans = find4();
        if (i + ans > Ans)
        {
            Ans = i + ans;
            temp1 = i; temp2 = ans;
        }
    }
    for (j = 1; j <= 26; j++)
    {
        pre[j] = leap[1][j] - temp1*leap[2][j];
    }
    for (j = 1; j <= 26; j++)
    {
        pre[j] = pre[j] - temp2*leap[3][j];
    }
    for (i = 1; i <= temp1; i++)
        printf("%s", b);
    for (i = 1; i <= temp2; i++)
        printf("%s", c);
    for (i = 1; i <= 26; i++)
    {
        for (j = 1; j <= pre[i]; j++)
            printf("%c", i + 'a' -1);
    }
    cout << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值