Algorithm


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 ab, 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
Copy
aaa
a
b
output
Copy
aaa
input
Copy
pozdravstaklenidodiri
niste
dobri
output
Copy
nisteaadddiiklooprrvz
input
Copy
abbbaaccca
ab
aca
output
Copy
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.

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <iostream>
#include <string>
#include <algorithm>
#include <memory.h>

using namespace std;

int main() {
	string a, b, c;
	cin >> a >> b >> c;

	int fa[26] = {0}, fb[26] = {0}, fc[26] = {0};

	for(int i = 0; i < (int)a.length(); ++i)
		++fa[a[i] - 'a'];
	for(int i = 0; i < (int)b.length(); ++i)
		++fb[b[i] - 'a'];
	for(int i = 0; i < (int)c.length(); ++i)
		++fc[c[i] - 'a'];

	int cb = 0, cc = 0;

	for(int i = 0; i < 1e5 + 1; ++i) {
		int tb = 1e9, tc = 1e9;

		bool ok = true;
		for(int j = 0; j < 26; ++j)
			if(fb[j] != 0 && fa[j] < 1LL * fb[j] * i) {
				ok = false;
				break;
			}

		if(!ok)
			tb = 0;
		else
			tb = i;

		for(int j = 0; j < 26; ++j)
			if(fc[j] != 0)
				tc = min(1LL * tc, max(0LL, (fa[j] - (1LL * fb[j] * tb)) / fc[j]));

		if(tb + tc > cb + cc)
			cb = tb, cc = tc;
	}

	for(int i = 0; i < cb; ++i) cout << b;
	for(int i = 0; i < cc; ++i) cout << c;

	for(int i = 0; i < 26; ++i) {
		fa[i] -= (fb[i] * cb) + (fc[i] * cc);
		for(int j = 0; j < fa[i]; ++j)
			cout << char(i + 'a');
	}
	cout << endl;

	return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
aaa
a
b

Output

x
+
cmd
aaa
Advertisements

Demonstration


Codeforces Solution-ZgukistringZ-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+