您的位置:首页 > 其它

Codeforces 551B ZgukistringZ【思维+枚举】

2017-01-30 17:46 465 查看
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");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces 551B