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. mysql数据库忘记密码时如何修改(转)

    当我们忘记mysql数据库密码时我们就无法正常进入数据库,也就无法修改密码,那么这时该怎么修改密码呢,这里教大家一个简单常用修改密码的方式. (如果图简单快速修改密码的话,直接跳过查询步骤,依照图上执 ...

  2. swiper轮播控件配置项

    var mySwiper = new Swiper ('.swiper-container', {    direction: 'horizontal',    loop: true,    auto ...

  3. vue.js的基础与语法

    Vue的实例 创建第一个实例: {{}} 被称之为插值表达式.可以用来进行文本插值. <!DOCTYPE html> <html lang="en"> &l ...

  4. CSS笔记 - fgm练习 2-8 - 简易日历

    <style> *{margin: 0; padding: 0} .outer{ width: 240px; margin: 10px auto; background: #f0f0f0; ...

  5. <meta name="viewport" content="width=device-width,initial-scale=1.0">

    meta name="viewport" content="width=device-width,initial-scale=1.0" 解释  <meta ...

  6. SpringMVC,采用的是SpringJDBC

    上一次复习搭建了SpringMVC+Mybatis,这次搭建一下SpringMVC,采用的是SpringJDBC,没有采用任何其他的ORM框 架,SpringMVC提供了一整套的WEB框架,所以如果想 ...

  7. (7)Launcher3客制化之,改动单屏幕后,Fix在Hotseat拖动应用删除报错

    改动单屏幕后,在workspace里面拖动图标.到删除button上松开的时候,报错问题. 而且无法再次显示拖动的图标. 拖动松开手时候触发 public void onDropCompleted(f ...

  8. [React Intl] Render Content with Markup Using react-intl FormattedHTMLMessage

    In this lesson, we’ll use the react-intl FormattedHTMLMessage component to display text with dynamic ...

  9. 通达OA 小飞鱼在线开发培训第一讲介绍(图文)

    培训课件的主要内容.须要參加培训的同学要注意了.课程内容以有用为主.课件仅供參考.

  10. WCF学习笔记——对象序列化

    当试图通过Web服务.WCF这样的远程处理技术将一个对象复制到远端时,具有对类型序列化的能力很关键. 一 序列化基础 序列化描述了持久化或传输一个对象的状态到流的过程(.NET将对象序列化到流,流是字 ...