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
Output
Input
Output
Input
Output
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");
}
}
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 593B Anton and Lines 【思维】【枚举】【排序】
- Codeforces 846C Four Segments【思维+预处理+前缀和枚举】
- Codeforces 466B Wonder Room【思维+暴力枚举】直觉题
- Codeforces 769D k-Interesting Pairs Of Integers【思维+预处理+暴力枚举】
- Codeforces 672C Recycling Bottles【极限思维+贪心枚举】
- Codeforces 544D Destroying Roads【思维枚举+最短路】好题~好题!
- Codeforces 599D Spongebob and Squares【思维枚举+数学方程】
- Codeforces Round #378 (Div. 2) && codeforces 733D(思维枚举)
- Codeforces 426D Sereja and Table【思维+暴力枚举】好题!
- Codeforces 485D Maximum Value【思维+技巧枚举】
- Codeforces 552D Vanya and Triangles【暴力枚举+思维】
- Codeforces 914C - Travelling Salesman and Special Numbers 【思维】
- CodeForces 540B School Marks(思维)
- codeforces 509C C. Sums of Digits(数位dp+贪心+枚举)
- CodeForces - 877B Nikita and string 【枚举区间】
- codeforces 876F High Cry (思维,位运算)
- codeforces 71C C. Round Table Knights(枚举)
- CodeForces - 776C 思维+普通的前缀和
- codeforces 560C Gerald's Hexagon (数学+思维)
- CodeForces - 931E Game with String (思维)