Codeforces 551B ZgukistringZ【思维+枚举】

本文介绍了一个字符串处理问题,目标是通过合理地替换原始字符串中的字符,使其包含尽可能多的指定子字符串,同时避免子字符串间的重叠。文章提供了一种解决策略,并附带了实现这一策略的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. ZgukistringZ
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
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.


题目大意:

给你一个原串,两个子串,然你找到一个结果串,使得其中连续子串包含尽可能多的两个给出的子串(不统计重叠区间)。


思路:


1、我们首先统计出来原串每个字符都出现了多少次,接下来暴力判断一下,原串最多可以组合出来多少个子串A,maxn。


2、接下来枚举组合出来子串A的数量【0,maxn】,此时暴力统计剩下的部分能够组合出来子串B的数量,过程维护最大和。

输出结果即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int vis[500];
int vis2[500];
int vis3[500];
int tmp[500];
char a[100060];
char b[100060];
char c[100600];
int main()
{
    while(~scanf("%s%s%s",a,b,c))
    {
        memset(vis2,0,sizeof(vis2));
        memset(vis,0,sizeof(vis));
        int len1=strlen(a);
        int len2=strlen(b);
        int len3=strlen(c);
        for(int i=0;i<len1;i++)vis[a[i]]++;
        for(int i=0;i<len2;i++)vis2[b[i]]++;
        for(int i=0;i<len3;i++)vis3[c[i]]++;
        int minn=0x3f3f3f3f;
        for(int i=0;i<450;i++)
        {
            if(vis2[i]>0&&vis[i]==0)minn=0;
            if(vis2[i]>0&&vis[i]>0)
            {
                minn=min(minn,vis[i]/vis2[i]);
            }
        }
        int ans1=0;int ans2=0;
        int maxn=0;
        for(int i=0;i<=minn;i++)
        {
            for(int j=0;j<450;j++)
            {
                tmp[j]=vis[j]-vis2[j]*i;
            }
            int tmpminn=0x3f3f3f3f;
            for(int j=0;j<450;j++)
            {
                if(vis3[j]>0&&tmp[j]==0)tmpminn=0;
                if(vis3[j]>0&&tmp[j]>0)
                {
                    tmpminn=min(tmpminn,tmp[j]/vis3[j]);
                }
            }
            if(maxn<i+tmpminn)
            {
                maxn=i+tmpminn;
                ans1=i;
                ans2=tmpminn;
            }
        }
        for(int i=0;i<ans1;i++)printf("%s",b);
        for(int i=0;i<ans2;i++)printf("%s",c);
        for(int i=0;i<450;i++)
        {
            vis[i]-=vis2[i]*ans1;
            vis[i]-=vis3[i]*ans2;
            for(int j=0;j<vis[i];j++)
            {
                printf("%c",i);
            }
        }
        printf("\n");
    }
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值