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?
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.
Find one of possible strings k, as described in the problem statement. If there are multiple possible answers, print any of them.
Sample test(s)
aaa a b
aaa
pozdravstaklenidodiri niste dobri
nisteaadddiiklooprrvz
abbbaaccca ab aca
ababacabcc
解题思路:一开始没看懂题意一直WA,原来发现就是一暴力枚举,首先枚举第2个串出现的次数,计算此时第3个串能够出现的次数,取所有情况两个串总共出现的次数的最大值,最后直接输出即可。
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <algorithm>
#include <functional>
using namespace std;
const int maxn = 100010;
int c1[30], c2[30], c3[30];
char s1[maxn], s2[maxn], s3[maxn];
int main() {
//freopen("aa.in", "r", stdin);
int x1 = 0x3f3f3f3f, x2;
int max_num, x, y;
scanf("%s", s1);
scanf("%s", s2);
scanf("%s", s3);
int l1 = strlen(s1);
int l2 = strlen(s2);
int l3 = strlen(s3);
memset(c1, 0, sizeof(c1));
memset(c2, 0, sizeof(c2));
memset(c3, 0, sizeof(c3));
for(int i = 0; i < l1; ++i) {
c1[s1[i]-'a']++;
}
for(int i = 0; i < l2; ++i) {
c2[s2[i]-'a']++;
}
for(int i = 0; i < l3; ++i) {
c3[s3[i]-'a']++;
}
for(int i = 0; i < 26; ++i) {
if(c2[i] > 0) {
x1 = min(x1, c1[i] / c2[i]);
}
}
max_num = 0;
x = 0;
y = 0;
for(int i = 0; i <= x1; ++i) {
x2 = 0x3f3f3f3f;
for(int j = 0; j < 26; ++j) {
if(c3[j] > 0) {
x2 = min(x2, (c1[j] - i * c2[j]) / c3[j]);
}
}
if(i + x2 > max_num) {
max_num = i + x2;
x = i, y = x2;
}
}
for(int i = 0; i < x; ++i) {
printf("%s", s2);
}
for(int i = 0; i < y; ++i) {
printf("%s", s3);
}
for(int i = 0; i < 26; ++i) {
int t = c1[i] - x * c2[i] - y * c3[i];
for(int j = 0; j < t; ++j) {
printf("%c", 'a'+i);
}
}
printf("\n");
return 0;
}