time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

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

【题目链接】:http://codeforces.com/contest/551/problem/B

【题解】



题中说a串可以任意交换两个字符。

其实就是说a串可以变成任意的字符;

所以原题意等价于给你a..z的26个字母,每个字母有若干个.

然后问你最多用这些字母组成几个b和c串.

剩余的字母随便输出就好.

可以这样。先全部给b用,全部给c用。

获取b串和c串最多能组成几个.

然后枚举b串组成了几个

再二分c串组成了几个.

l r

m = (l+r)>>1;

显然如果m情况下字母够用则可以增大m.否则减小m

而判断i个b串j个c串是否可以用所给字母表示则可以在O(26)搞出来;

所以复杂度就接近O(NlogN)吧。没问题的。



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; //const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0); string a,b,c; map <char,int> dic,dic1,dic2; int main()
{
//freopen("F:\\rush.txt","r",stdin);
cin >> a;
cin >> b;
cin >> c;
int lena = a.size();
rep1(i,0,lena-1)
dic[a[i]]++;
int lenb = b.size();
rep1(i,0,lenb-1)
dic1[b[i]]++;
int lenc = c.size();
rep1(i,0,lenc-1)
dic2[c[i]]++;
int num1 = 0,num2 = 0;
int mi=21e8;
rep1(i,1,26)
{
char ke = i+'a'-1;
if (dic1[ke] > 0)
mi = min(mi,dic[ke]/dic1[ke]);
}
num1 = mi;
mi=21e8;
rep1(i,1,26)
{
char ke = i+'a'-1;
if (dic2[ke] > 0)
mi = min(mi,dic[ke]/dic2[ke]);
}
num2 = mi;
int ans1 = 0,ans2 = 0,ans = 0;
rep1(i,0,num1)
{
int l = 0,r = num2,j=-1;
while (l <= r)
{
int m = (l+r)>>1;
bool ok = true;
rep1(k,1,26)
{
char ke = k+'a'-1;
int numi = i*dic1[ke]+m*dic2[ke];
if (numi>dic[ke])
{
ok = false;
break;
}
}
if (ok)
{
j = m,l = m+1;
}
else
r = m-1;
}
if (j!=-1 && ans < i+j)
{
ans1 = i;ans2 = j;
ans = i+j;
}
if (j==-1) break;
}
rep1(i,1,26)
{
char ke = i+'a'-1;
int numi = ans1*dic1[ke]+ans2*dic2[ke];
dic[ke]-=numi;
}
string s = "";
rep1(i,1,ans1)
s+=b;
rep1(i,1,ans2)
s+=c;
rep1(i,1,26)
{
char ke = i+'a'-1;
while (dic[ke]>0)
{
dic[ke]--;
s+=ke;
}
}
cout << s<<endl;
return 0;
}

【19.46%】【codeforces 551B】ZgukistringZ的更多相关文章

  1. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  2. 【19.77%】【codeforces 570D】Tree Requests

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【44.19%】【codeforces 608D】Zuma

    time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...

  4. 【codeforces 46C】Hamsters and Tigers

    [题目链接]:http://codeforces.com/problemset/problem/46/C [题意] 给你一个长度为n的01串; 让你把所有的0放在一起,把所有的1放在一起; (即0都是 ...

  5. 【77.78%】【codeforces 625C】K-special Tables

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

  6. 【30.43%】【codeforces 746C】Tram

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  8. 【.NET】传智播客第【19】期就业班视频(高清无加密)

    [.NET]传智播客第[19]期就业班视频(高清无加密) 下载地址:http://fu83.cn/thread-85-1-1.html

  9. 【搜索】【并查集】Codeforces 691D Swaps in Permutation

    题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...

随机推荐

  1. C# Find() 与 FindAll()方法的使用

    Find()   :检索与指定匹配的第一个元素 FindAll()   : 检索与指定匹配的所有元素 如:List<string> strList=new List<string&g ...

  2. BZOJ4196: [Noi2015]软件包管理器(树链剖分)

    Description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖( ...

  3. 82.管道实现cgi内存多线程查询

    总体思路就是客户端写入要查询的数据到管道中,服务器端从管道读取,然后写入随机文件,再把文件名写入管道,然后客户端再读取文件 服务器端 设置缓冲区大写,设置管道名字,以及标识有多少个线程等 //设置缓存 ...

  4. robotframework Selenium2+RFS自动化测试

    支持浏览器版本:Google Chrome (64位) 52.0.2743.82 正式版 52.0.2743.6_chrome_installer 64位 下载地址:http://www.online ...

  5. C# SocketAsyncEventArgs类

    Namespace:System.Net.Sockets Assemblies:System.Net.Sockets.dll, System.dll, netstandard.dll (Represe ...

  6. angular 创建服务

    一:新建服务模块和服务文件 ng g module services --spec=false ng g service services/quote --spec=false 二:在quote.se ...

  7. (转)windows 下 Java 及 Python 环境变量设置

    转自:http://www.cnblogs.com/zhj5chengfeng/archive/2013/01/01/2841253.html http://www.cnblogs.com/qiyes ...

  8. SQL Server2008 Hierarchyid数据类型

    以往我们在关系数据库中建立树状结构的时候,通常使用ID+ParentID来实现两条 纪录间的父子关系.但这种方式只能标示其相对位置.解决这类问题在SqlServer2005出现之前通常是采用游标来操作 ...

  9. 从反编译深入理解JAVA内部类类结构以及finalkeyword

    1.为什么成员内部类能够无条件訪问外部类的成员? 在此之前,我们已经讨论过了成员内部类能够无条件訪问外部类的成员,那详细到底是怎样实现的呢?以下通过反编译字节码文件看看到底.其实,编译器在进行编译的时 ...

  10. C语言结构体的字节对齐原则

    为什么要对齐? 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特 定的内存地址访问,这就需要各种类型数据 ...