text
stringlengths
49
983k
#include <bits/stdc++.h> void quit(); using namespace std; const long double PI = acos((double)-1.0); const long double EPS = 1e-11; double __t; string s1, s2, s3; long long k1[30], k2[30], k3[30]; long long ans, t, a1, a2; long long cnt() { long long mmax = 999999; for (int i = 0; i < (int)26; ++i) if (k3[i] > 0) mmax = min(mmax, k1[i] / k3[i]); return mmax; } int main() { cin >> s1 >> s2 >> s3; for (int i = 0; i < (int)s1.size(); ++i) k1[s1[i] - 'a']++; for (int i = 0; i < (int)s2.size(); ++i) k2[s2[i] - 'a']++; for (int i = 0; i < (int)s3.size(); ++i) k3[s3[i] - 'a']++; for (int i = 0; i < (int)100001; ++i) { bool can = 1; for (int j = 0; j < (int)26; ++j) k1[j] -= i * k2[j]; for (int j = 0; j < (int)26; ++j) if (k1[j] < 0) can = 0; if (can) { t = cnt(); if (i + t > ans) { ans = i + t; a1 = i; a2 = t; } } for (int j = 0; j < (int)26; ++j) k1[j] += i * k2[j]; } for (int i = 0; i < (int)26; ++i) { k1[i] -= k2[i] * a1; k1[i] -= k3[i] * a2; } for (int i = 0; i < (int)a1; ++i) cout << s2; for (int i = 0; i < (int)a2; ++i) cout << s3; for (int i = 0; i < (int)26; ++i) for (int j = 0; j < (int)k1[i]; ++j) cout << (char)(i + 'a'); quit(); } void quit() { exit(0); }
#include <bits/stdc++.h> using namespace std; char a[100005] = {0}, b[100005] = {0}, c[100005] = {0}; int cnt[27] = {0}, cnt2[27] = {0}, cnt3[27] = {0}; int main() { scanf("%s", a); scanf("%s", b); scanf("%s", c); int lena = strlen(a); int lenb = strlen(b); int lenc = strlen(c); for (int i = 0; i < lena; i++) { cnt[a[i] - 'a' + 1]++; } for (int i = 0; i < lenb; i++) { cnt2[b[i] - 'a' + 1]++; } for (int i = 0; i < lenc; i++) { cnt3[c[i] - 'a' + 1]++; } int ma = -1, ans = 0, ans2 = 0; for (int i = 0; i <= lena / lenb; i++) { int okay = 1; for (int j = 1; j <= 26; j++) { if (cnt[j] - i * cnt2[j] < 0) { okay = 0; break; } } if (okay) { int num = 0, mi = 99999999; for (int k = 1; k <= 26; k++) { if (cnt3[k]) num = (cnt[k] - i * cnt2[k]) / cnt3[k]; else num = 0; if (cnt3[k] && num < mi) mi = num; } if (mi + i > ma) { ma = mi + i; ans = i; ans2 = mi; } } else break; } for (int i = 1; i <= ans; i++) { printf("%s", b); } for (int i = 1; i <= ans2; i++) { printf("%s", c); } for (int j = 1; j <= 26; j++) cnt[j] -= ans2 * cnt3[j] + ans * cnt2[j]; for (int i = 1; i <= 26; i++) { while (cnt[i]) { printf("%c", i - 1 + 'a'); cnt[i]--; } } return 0; }
#include <bits/stdc++.h> char a[100005]; char b[100005]; char c[100005]; int AA[30]; int A[30]; int B[30]; int C[30]; int main(void) { int i, j; int lenA, lenB, lenC; int cntB, cntC, max, mB, mC; bool chk; scanf("%s%s%s", a, b, c); lenA = strlen(a); lenB = strlen(b); lenC = strlen(c); for (i = 0; i < lenA; i++) A[a[i] - 'a']++; for (i = 0; i < lenB; i++) B[b[i] - 'a']++; for (i = 0; i < lenC; i++) C[c[i] - 'a']++; for (i = 0; i < 26; i++) AA[i] = A[i]; cntB = cntC = max = 0; while (1) { chk = true; for (i = 0; i < 26; i++) { if (A[i] < B[i]) chk = false; } if (!chk) break; for (i = 0; i < 26; i++) A[i] -= B[i]; cntB++; } max = cntB; mB = cntB; mC = 0; while (1) { while (1) { chk = true; for (i = 0; i < 26; i++) { if (A[i] < C[i]) chk = false; } if (!chk) break; for (i = 0; i < 26; i++) A[i] -= C[i]; cntC++; } if (max < cntB + cntC) { max = cntB + cntC; mB = cntB; mC = cntC; } if (cntB == 0) break; for (i = 0; i < 26; i++) A[i] += B[i]; cntB--; } for (i = 0; i < mB; i++) printf("%s", b); for (i = 0; i < mC; i++) printf("%s", c); for (i = 0; i < 26; i++) { AA[i] -= B[i] * mB; AA[i] -= C[i] * mC; for (j = 0; j < AA[i]; j++) printf("%c", i + 'a'); } printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; string s1, s2, s3; int h1[30], h2[30], h3[30]; int cnt2, cnt3, cnt; int main() { int i, j; cin >> s1 >> s2 >> s3; int len1 = s1.size(), len2 = s2.size(), len3 = s3.size(); memset(h1, 0, sizeof(h1)); memset(h2, 0, sizeof(h2)); memset(h3, 0, sizeof(h3)); for (i = 0; i < len1; i++) h1[s1[i] - 'a']++; for (i = 0; i < len2; i++) h2[s2[i] - 'a']++; for (i = 0; i < len3; i++) h3[s3[i] - 'a']++; cnt = 0; for (i = 1;; i++) { int p = 100005; for (j = 0; j < 26; j++) { if (h1[j] - h2[j] * i < 0) { p = 0; break; } } if (!p) break; int p2 = 100005; for (j = 0; j < 26; j++) { if (h3[j]) p2 = min(p2, (h1[j] - h2[j] * i) / h3[j]); } if (i + p2 > cnt) { cnt = i + p2; cnt2 = i; cnt3 = p2; } } for (i = 1;; i++) { int p = 100005; for (j = 0; j < 26; j++) { if (h1[j] - h3[j] * i < 0) { p = 0; break; } } if (!p) break; int p2 = 100005; for (j = 0; j < 26; j++) { if (h2[j]) p2 = min(p2, (h1[j] - h3[j] * i) / h2[j]); } if (i + p2 > cnt) { cnt = i + p2; cnt3 = i; cnt2 = p2; } } for (i = 0; i < cnt2; i++) cout << s2; for (i = 0; i < cnt3; i++) cout << s3; for (i = 0; i < 26; i++) { for (j = 0; j < h1[i] - cnt2 * h2[i] - cnt3 * h3[i]; j++) cout << char(i + 'a'); } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long n; map<char, long long> aa, bb, cc; string a, b, c; bool prov(long long x, long long y) { bool flag = 0; for (map<char, long long>::iterator p = bb.begin(); p != bb.end(); p++) { if (x * p->second + y * cc[p->first] > aa[p->first]) { flag = 1; break; } } for (map<char, long long>::iterator p = cc.begin(); p != cc.end(); p++) { if (y * p->second + x * bb[p->first] > aa[p->first]) { flag = 1; break; } } if (!flag) return true; return false; } long long find_1(int k, int l = 0, int r = 100001) { while (l + 1 < r) { long long cur = (l + r) / 2; if (prov(k, cur)) l = cur; else r = cur; } if (prov(k, l)) return l; else return -1; } void find_2() { long long l = 0, r = 100000; long long ans = 0; for (int i = 0; i < a.size() / b.size() + 1; i++) { long long t = find_1(i); if (t >= 0 && i + t > ans) l = i, ans = i + t; } for (int i = 0; i < l; i++) cout << b; for (int i = 0; i < ans - l; i++) cout << c; for (map<char, long long>::iterator p = aa.begin(); p != aa.end(); p++) { for (int i = 0; i < p->second - bb[p->first] * l - cc[p->first] * (ans - l); i++) cout << p->first; } } int main() { cin >> a >> b >> c; for (int i = 0; i < a.size(); i++) { aa[a[i]]++; } for (int i = 0; i < b.size(); i++) { bb[b[i]]++; } for (int i = 0; i < c.size(); i++) { cc[c[i]]++; } find_2(); return 0; }
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double EPS = 1e-6; char s[3][100005]; int cnt[3][26]; int len[3]; int GetMaxCount(int *remain, int *arr) { int res = ((1 << 30) - 1); for (int k = 0; k < 26; k++) if (arr[k] > 0) res = min(res, remain[k] / arr[k]); return res; } int main() { { ; }; for (int k = 0; k < 3; k++) { gets(s[k]); len[k] = strlen(s[k]); for (int i = 0; i < len[k]; i++) cnt[k][s[k][i] - 'a']++; } int res = 0; pair<int, int> p = pair<int, int>(0, 0); int bMaxCount = GetMaxCount(cnt[0], cnt[1]); for (int bCount = 0; bCount <= bMaxCount; bCount++) { int remain[26]; memmove(remain, cnt[0], sizeof(remain)); for (int k = 0; k < 26; k++) remain[k] -= cnt[1][k] * bCount; int cCount = GetMaxCount(remain, cnt[2]); if (bCount + cCount > res) { res = bCount + cCount; p = pair<int, int>(bCount, cCount); } } int remain[26]; memmove(remain, cnt[0], sizeof(remain)); for (int k = 0; k < 26; k++) remain[k] -= cnt[1][k] * p.first + cnt[2][k] * p.second; for (int b = 0; b < p.first; b++) printf("%s", s[1]); for (int c = 0; c < p.second; c++) printf("%s", s[2]); for (int k = 0; k < 26; k++) for (int a = 0; a < remain[k]; a++) printf("%c", k + 'a'); puts(""); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100010; int n, m, k, ba[26], bb[26], bs[26]; int d, res, prvi, drugi; char s[N], a[N], b[N], resenje[N]; int main() { scanf("%s %s %s", s + 1, a + 1, b + 1); n = strlen(s + 1); m = strlen(a + 1); k = strlen(b + 1); for (int i = 1; i <= n; i++) bs[s[i] - 'a']++; for (int i = 1; i <= m; i++) ba[a[i] - 'a']++; for (int i = 1; i <= k; i++) bb[b[i] - 'a']++; for (int i = 0;; i++) { int x = n; for (int j = 0; j < 26; j++) if (bb[j]) x = min(x, bs[j] / bb[j]); if (x + i > res) { res = x + i; prvi = i; drugi = x; } bool f = false; for (int j = 0; j < 26; j++) if (bs[j] < ba[j]) { f = true; break; } else bs[j] -= ba[j]; if (f) break; } for (int i = 1; i <= prvi; i++) for (int j = 1; j <= m; j++) resenje[++d] = a[j]; for (int i = 1; i <= drugi; i++) for (int j = 1; j <= k; j++) resenje[++d] = b[j]; for (int i = 0; i < 26; i++) bs[i] = -ba[i] * prvi - bb[i] * drugi; for (int i = 1; i <= n; i++) bs[s[i] - 'a']++; for (int i = 0; i < 26; i++) for (int j = 1; j <= bs[i]; j++) resenje[++d] = 'a' + i; for (int i = 1; i <= n; i++) printf("%c", resenje[i]); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout << fixed; string a, b, c; cin >> a >> b >> c; vector<int> va(26), vb(26), vc(26); for (char x : a) va[x - 'a']++; for (char x : b) vb[x - 'a']++; for (char x : c) vc[x - 'a']++; int maxsum = 0, maxi = 0, maxj = 0; bool stop = false; for (int i = 0; i <= a.size() && !stop; ++i) { int j = a.size(); for (int x = 0; x < 26; ++x) { if (vc[x]) j = min(j, va[x] / vc[x]); } if (maxsum < i + j) { maxsum = i + j; maxi = i; maxj = j; } for (int x = 0; x < 26; ++x) { if (va[x] < vb[x]) { stop = true; break; } va[x] -= vb[x]; } } string ans; for (int i = 0; i < maxi; ++i) ans += b; for (int j = 0; j < maxj; ++j) ans += c; va.assign(va.size(), 0); for (char x : a) va[x - 'a']++; for (int x = 0; x < 26; ++x) { va[x] -= maxi * vb[x] + maxj * vc[x]; for (int k = 0; k < va[x]; ++k) ans += (char)(x + 'a'); } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:167772160") using namespace std; const double PI = 3.14159265358979323846; const long long MODUL = 1000000007; const long long MAXINT = 2e9 + 2; const long long MAXLL = 2e18 + 2; const double eps = 1e-9; const int MAXN = 1e6 + 10; int main() { ios_base::sync_with_stdio(0), cin.tie(0); string s, sb, sc; int a[256], b[256], c[256], mres(0), bcou(0); for (int i(0); i < 256; ++i) a[i] = b[i] = c[i] = 0; cin >> s; for (int i(0); i < s.size(); ++i) ++a[s[i]]; cin >> s; sb = s; for (int i(0); i < s.size(); ++i) ++b[s[i]]; cin >> s; sc = s; for (int i(0); i < s.size(); ++i) ++c[s[i]]; for (int i(0); i < 100003; ++i) { int ma = MAXINT; bool f(0); for (int j(0); j < 256 && !f; ++j) if (a[j] < b[j] * i) f = 1; if (f) break; for (int j(0); j < 256; ++j) if (c[j]) ma = min(ma, (a[j] - b[j] * i) / c[j]); if (ma + i > mres) { mres = ma + i; bcou = i; } } for (int i(0); i < bcou; ++i) cout << sb; for (int i(0); i < mres - bcou; ++i) cout << sc; for (char i(0); i < 127; ++i) { a[i] -= b[i] * bcou + c[i] * (mres - bcou); for (; a[i]-- > 0; cout << i) ; } return 0; }
#include <bits/stdc++.h> using namespace std; int i, j, k, n, m, x, y, T, ans, big, cas, num, len; bool flag; char a[100015], b[100015], c[100015]; int pa[30], pb[30], pc[30]; int main() { scanf("%s", a); scanf("%s", b); scanf("%s", c); int lena = strlen(a); for (i = 0; i < lena; i++) { pa[a[i] - 'a']++; } int lenb = strlen(b); for (i = 0; i < lenb; i++) { pb[b[i] - 'a']++; } int lenc = strlen(c); for (i = 0; i < lenc; i++) { pc[c[i] - 'a']++; } int mib = 0x3fffffff; for (i = 0; i < 26; i++) { if (pb[i] == 0) continue; mib = min(mib, pa[i] / pb[i]); } ans = 0; int ansi = 0, ansj = 0; for (i = 0; i <= mib; i++) { int mic = 0x3fffffff; for (j = 0; j < 26; j++) { if (pc[j] == 0) continue; mic = min(mic, (pa[j] - pb[j] * i) / pc[j]); } if (ansi + ansj < i + mic) { ansi = i; ansj = mic; } } for (i = 0; i < ansi; i++) printf("%s", b); for (i = 0; i < ansj; i++) printf("%s", c); for (i = 0; i < 26; i++) { int t = pa[i] - ansi * pb[i] - ansj * pc[i]; for (j = 0; j < t; j++) printf("%c", i + 'a'); } printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const double eps = 1e-8; const int maxN = 5e3 + 10; const int maxL = 45; const int inf = 1e9 + 10; string a[3]; int cnt[26][3]; inline void init() { cin >> a[0] >> a[1] >> a[2]; for (int i = 0; i < 3; i++) for (int j = 0; j < a[i].size(); j++) cnt[a[i][j] - 'a'][i]++; } inline void solve() { int ans = 0, c1 = 0; for (int i = 0; i * a[1].size() <= a[0].size(); i++) { bool pos = true; for (int j = 0; j < 26; j++) { int k1 = cnt[j][0], k2 = i * cnt[j][1]; if (k1 < k2) { pos = false; break; } } if (!pos) break; int mn = inf; for (int j = 0; j < 26; j++) { if (cnt[j][2] != 0) mn = min(mn, (cnt[j][0] - (i * cnt[j][1])) / cnt[j][2]); } if (ans < mn + i) { ans = mn + i; c1 = i; } } for (int i = 0; i < c1; i++) cout << a[1]; for (int i = 0; i < ans - c1; i++) cout << a[2]; for (int i = 0; i < 26; i++) cnt[i][0] -= cnt[i][1] * c1 + cnt[i][2] * (ans - c1); for (int i = 0; i < 26; i++) for (int j = 0; j < cnt[i][0]; j++) cout << (char)('a' + i); cout << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); init(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string A, B, C; cin >> A >> B >> C; int arrayA[26]; int arrayB[26]; int arrayC[26]; memset(arrayA, 0, sizeof(arrayA)); memset(arrayB, 0, sizeof(arrayB)); memset(arrayC, 0, sizeof(arrayC)); for (int i = 0; i < A.length(); i++) { arrayA[(A[i] - 'a')]++; } for (int i = 0; i < B.length(); i++) { arrayB[(B[i] - 'a')]++; } for (int i = 0; i < C.length(); i++) { arrayC[(C[i] - 'a')]++; } int minB = INT_MAX; for (int i = 0; i < 26; i++) { if (arrayB[i]) { minB = arrayA[i] / arrayB[i] < minB ? arrayA[i] / arrayB[i] : minB; } } int minC = INT_MAX; for (int i = 0; i < 26; i++) { if (arrayC[i]) { minC = arrayA[i] / arrayC[i] < minC ? arrayA[i] / arrayC[i] : minC; } } int remainC = INT_MAX; for (int i = 0; i < 26; i++) { if (arrayC[i]) { remainC = (arrayA[i] - arrayB[i] * minB) / arrayC[i] < remainC ? (arrayA[i] - arrayB[i] * minB) / arrayC[i] : remainC; } } int remainB = INT_MAX; for (int i = 0; i < 26; i++) { if (arrayB[i]) { remainB = (arrayA[i] - arrayC[i] * minC) / arrayB[i] < remainB ? (arrayA[i] - arrayC[i] * minC) / arrayB[i] : remainB; } } string s; int max = 0; int bestB = 0; for (int x = 0; x <= minB; x++) { int temp = INT_MAX; for (int i = 0; i < 26; i++) { if (arrayC[i]) { temp = (arrayA[i] - arrayB[i] * x) / arrayC[i] < temp ? (arrayA[i] - arrayB[i] * x) / arrayC[i] : temp; } } if (x + temp > max) { max = x + temp; bestB = x; } } for (int i = 0; i < bestB; i++) { s = s + B; } for (int i = 0; i < max - bestB; i++) { s = s + C; } for (int i = 0; i < 26; i++) { arrayA[i] = arrayA[i] - (arrayB[i] * bestB) - (arrayC[i] * (max - bestB)); } char c; for (int i = 0; i < 26; i++) { while (arrayA[i]--) { c = 'a' + i; s = s + c; } } cout << s; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const int inf = 0x3f3f3f3f; char a[N], b[N], c[N]; int len1, len2, len3; int cnt1[26], cnt2[26], cnt3[26], ans[26]; bool work(int x[], int y[], int w, int &ans1, int &ans2) { int num[26]; for (int i = 0; i < 26; i++) num[i] = cnt1[i]; int res = inf; for (int i = 0; i < 26; i++) if (x[i] * w > num[i]) return false; for (int i = 0; i < 26; i++) num[i] -= w * x[i]; for (int i = 0; i < 26; i++) if (y[i]) res = min(res, num[i] / y[i]); for (int i = 0; i < 26; i++) num[i] -= res * y[i]; if (w + res > ans1 + ans2) { ans1 = w; ans2 = res; for (int i = 0; i < 26; i++) ans[i] = num[i]; } return true; } int main() { scanf("%s%s%s", a, b, c); len1 = strlen(a); len2 = strlen(b); len3 = strlen(c); for (int i = 0; i < len1; i++) cnt1[a[i] - 'a']++; for (int i = 0; i < len2; i++) cnt2[b[i] - 'a']++; for (int i = 0; i < len3; i++) cnt3[c[i] - 'a']++; for (int i = 0; i < 26; i++) ans[i] = cnt1[i]; int ans1 = 0, ans2 = 0; for (int i = 0; i <= len1; i++) { if (!work(cnt2, cnt3, i, ans1, ans2)) break; } for (int i = 1; i <= ans1; i++) printf("%s", b); for (int i = 1; i <= ans2; i++) printf("%s", c); for (int i = 0; i < 26; i++) for (int j = 1; j <= ans[i]; j++) printf("%c", 'a' + i); puts(""); return 0; }
#include <bits/stdc++.h> using namespace std; char a[100010], b[100010], c[100010]; int cnta[30], cntb[30], cntc[30]; int main() { scanf("%s%s%s", a, b, c); int l1 = strlen(a), l2 = strlen(b), l3 = strlen(c); for (int i = 0; i < l1; i++) cnta[a[i] - 'a']++; for (int i = 0; i < l2; i++) cntb[b[i] - 'a']++; for (int i = 0; i < l3; i++) cntc[c[i] - 'a']++; int maxx, maxy; maxx = maxy = 0; int sum = 0; for (int x = 0; x <= l1 / l2; x++) { int y = 1000000, flag = 1; for (int i = 0; i < 26; i++) { if (cnta[i] - x * cntb[i] < 0) flag = 0; } if (flag == 0) break; for (int i = 0; i < 26; i++) { if (cntc[i] == 0) continue; int z = (cnta[i] - x * cntb[i]) / cntc[i]; if (y > z) y = z; } if (x + y > sum) { sum = x + y; maxx = x; maxy = y; } } for (int i = 0; i < 26; i++) { cnta[i] -= (maxx * cntb[i] + maxy * cntc[i]); } for (int i = 0; i < maxx; i++) printf("%s", b); for (int i = 0; i < maxy; i++) printf("%s", c); for (int i = 0; i < 26; i++) { for (int j = 0; j < cnta[i]; j++) printf("%c", i + 'a'); } printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string a, b, c; cin >> a >> b >> c; long long nb = a.length() + 1, nc, sol, solb, solc; map<char, long long> ma, mb, mc, cacac, cacab, aux; for (int i = 0; i < a.length(); i++) ma[a[i]]++, mb[a[i]]++, mc[a[i]]++; for (int i = 0; i < b.length(); i++) cacab[b[i]]++; for (int i = 0; i < c.length(); i++) cacac[c[i]]++; for (int i = 0; i < b.length(); i++) nb = min(nb, mb[b[i]] / cacab[b[i]]); sol = nb; solb = nb; solc = 0; for (int i = 0; i <= nb; i++) { aux = mb; for (int e = 0; e < b.length(); e++) aux[b[e]] -= i; nc = a.length() + 1; for (int e = 0; e < c.length(); e++) nc = min(nc, aux[c[e]] / cacac[c[e]]); if (sol < i + nc) { sol = i + nc; solb = i; solc = nc; } } for (int i = 0; i < b.length(); i++) ma[b[i]] -= solb; for (int i = 0; i < c.length(); i++) ma[c[i]] -= solc; for (int i = 0; i < a.length(); i++) if (ma[a[i]] > 0) { cout << a[i]; ma[a[i]]--; } while (solb--) cout << b; while (solc--) cout << c; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int m, long long int n) { if (n == 0) return m; return gcd(n, m % n); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); string a, b, c; cin >> a; cin >> b; cin >> c; int arr[26] = {0}; int barr[26] = {0}; int carr[26] = {0}; for (int i = 0; i < (int)(a.size()); i++) arr[a[i] - 'a']++; for (int i = 0; i < (int)(b.size()); i++) barr[b[i] - 'a']++; for (int i = 0; i < (int)(c.size()); i++) carr[c[i] - 'a']++; char f[100010]; int k = 0; while (1) { int fl1 = 0; int fl2 = 0; int bmin = INT_MAX; for (int i = 0; i < 26; i++) { if (arr[i] < barr[i]) { fl1 = 1; break; } if (arr[i] != 0 && barr[i] != 0) { bmin = min(bmin, arr[i] / barr[i]); } } int cmin = INT_MAX; for (int i = 0; i < 26; i++) { if (arr[i] < carr[i]) { fl2 = 1; break; } if (arr[i] != 0 && carr[i] != 0) { cmin = min(cmin, arr[i] / carr[i]); } } if (fl1 == 1 && fl2 == 1) break; else if (fl1 == 1) { for (int j = 0; j < (int)(c.size()); j++) { f[k] = c[j]; k++; arr[c[j] - 'a']--; } } else if (fl2 == 1) { for (int j = 0; j < (int)(b.size()); j++) { f[k] = b[j]; k++; arr[b[j] - 'a']--; } } else { if (cmin >= bmin) { for (int j = 0; j < (int)(c.size()); j++) { f[k] = c[j]; k++; arr[c[j] - 'a']--; } } else { for (int j = 0; j < (int)(b.size()); j++) { f[k] = b[j]; k++; arr[b[j] - 'a']--; } } } } for (int i = 0; i < 26; i++) { while (arr[i] > 0) { f[k] = 97 + i; k++; arr[i]--; } } for (int i = 0; i < (int)(a.size()); i++) cout << f[i]; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int ach[130], bch[130], cch[130]; int main() { string a, b, c; cin >> a >> b >> c; memset(ach, 0, sizeof(ach)); memset(bch, 0, sizeof(bch)); memset(cch, 0, sizeof(cch)); int len = a.size(); for (int i = 0; i < len; i++) ach[a[i]]++; len = b.size(); for (int i = 0; i < len; i++) bch[b[i]]++; len = c.size(); for (int i = 0; i < len; i++) cch[c[i]]++; int k = 0, ans = 0, ansk = 0; bool p = 1; while (p) { int l = 0x3f3f3f3f; for (int i = 'a'; i <= 'z'; i++) { if (ach[i] - k * bch[i] < 0) { p = 0; break; } if (cch[i] && l > k + (ach[i] - k * bch[i]) / cch[i]) l = k + (ach[i] - k * bch[i]) / cch[i]; } if (p && l > ans) { ans = l; ansk = k; } k++; } for (int i = 0; i < ansk; i++) cout << b; for (int i = 0; i < ans - ansk; i++) cout << c; for (int i = 'a'; i <= 'z'; i++) for (int j = 0; j < ach[i] - ansk * bch[i] - (ans - ansk) * cch[i]; j++) printf("%c", i); printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; double pi = 3.141592653589793238462; string numkey = "0123456789"; string uni = "abcdefghijklmnopqrstuvwxyz"; long long int dx[4] = {0, 0, 1, -1}; long long int dy[4] = {1, -1, 0, 0}; long long int INF = (1LL << 62); long long int calc(vector<long long int> &c1, vector<long long int> c2) { long long int mn = INF; for (long long int i = 0; i < 26; i++) { if (c1[i] == -1 && c2[i] != -1) { return 0; } if (c1[i] != -1 && c2[i] != -1) { mn = min(mn, c1[i] / c2[i]); } } for (long long int i = 0; i < 26; i++) { if (c2[i] != -1) c1[i] -= mn * c2[i]; } return mn; } signed main() { string a, b, c; cin >> a >> b >> c; vector<long long int> cnt1(26), cnt2(26), cnt3(26); for (long long int i = 0; i < a.length(); i++) { cnt1[a[i] - 'a']++; } for (long long int i = 0; i < b.length(); i++) { cnt2[b[i] - 'a']++; } for (long long int i = 0; i < c.length(); i++) { cnt3[c[i] - 'a']++; } vector<long long int> c1 = cnt1, c2 = cnt2, c3 = cnt3; long long int val = INF; for (long long int i = 0; i < 26; i++) { if (cnt2[i]) val = min(val, cnt1[i] / cnt2[i]); } long long int ans1 = 0, ans2 = 0; for (long long int i = 0; i <= val; i++) { long long int forb = 1000000; for (long long int j = 0; j < 26; j++) { if (cnt3[j]) forb = min(forb, (cnt1[j] - i * cnt2[j]) / cnt3[j]); } if (i + forb > ans1 + ans2) ans1 = i, ans2 = forb; } for (long long int i = 0; i < ans1; i++) { cout << b; } for (long long int j = 0; j < ans2; j++) cout << c; for (long long int i = 0; i < 26; i++) { cnt1[i] -= (cnt2[i] * ans1 + cnt3[i] * ans2); while (cnt1[i] > 0) { cnt1[i]--; cout << uni[i]; } } return 0; }
#include <bits/stdc++.h> char A[100000 + 1], B[100000 + 1], C[100000 + 1]; int vfa[30]; int vfb[30]; int vfc[30]; int main() { FILE *fi, *fout; int i, na, nb, nc, a, b, flag, min, sol, j; fi = stdin; fout = stdout; scanf("%s%s%s", &A, &B, &C); na = strlen(A); for (i = 0; i < na; i++) vfa[A[i] - 'a']++; nb = strlen(B); for (i = 0; i < nb; i++) vfb[B[i] - 'a']++; nc = strlen(C); for (i = 0; i < nc; i++) vfc[C[i] - 'a']++; sol = 0; a = b = 0; for (i = 0; i <= na; i++) { min = na + 1; flag = 0; for (j = 0; j < 26; j++) if (vfb[j] > 0) { if (1LL * vfb[j] * i <= vfa[j]) { if (vfc[j]) min = std::min((vfa[j] - i * vfb[j]) / vfc[j], min); } else flag = 1; } else if (vfc[j]) min = std::min(vfa[j] / vfc[j], min); if (!flag && sol < min + i) { sol = min + i; a = i; b = min; } } while (a > 0) { a--; for (i = 0; i < nb; i++) { printf("%c", B[i]); vfa[B[i] - 'a']--; } } while (b > 0) { b--; for (i = 0; i < nc; i++) { printf("%c", C[i]); vfa[C[i] - 'a']--; } } for (i = 0; i < 26; i++) while (vfa[i] > 0) { printf("%c", i + 'a'); vfa[i]--; } return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:667772160") using namespace std; struct __isoff { __isoff() { if (0) freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(0); srand(time(0)); } ~__isoff() {} } __osafwf; const unsigned long long p1 = 31; const unsigned long long p2 = 29; const double eps = 1e-8; const double pi = acos(-1.0); const long long inf = 1e18 + 7; const int infi = 1e9 + 7; const long long dd = 3e5 + 7; const long long mod = 1e9 + 7; int A[128], B[128], C[128]; int main() { string a, b, c; cin >> a >> b >> c; for (long long i = 0; i < (long long)a.size(); i++) A[a[i] - 'a']++; for (long long i = 0; i < (long long)b.size(); i++) B[b[i] - 'a']++; for (long long i = 0; i < (long long)c.size(); i++) C[c[i] - 'a']++; int res = -1, op, z = -1; for (long long i = 0; i < (long long)a.size() + 1; i++) { int s = infi; bool bsf = 0; for (long long j = 0; j < (long long)26; j++) { int q = A[j] - B[j] * i; if (q < 0) { s = -infi; bsf = 1; } if (C[j] > 0) s = min(s, q / C[j]); } if (bsf) break; if (res + z < s + i) res = s, op = i, z = i; } string k; for (long long i = 0; i < (long long)res; i++) { for (long long j = 0; j < (long long)c.size(); j++) k.push_back(c[j]); } for (long long i = 0; i < (long long)z; i++) { for (long long j = 0; j < (long long)b.size(); j++) k.push_back(b[j]); } for (long long i = 0; i < (long long)k.size(); i++) A[k[i] - 'a']--; for (long long i = 0; i < (long long)32; i++) while (A[i]) { A[i]--; k.push_back((char)(i + 'a')); } cout << k; }
#include <bits/stdc++.h> using namespace std; int cnt[3][27]; int lef[27]; char in[3][100100]; int needed[27]; int main(void) { for (int i = 0; i < 3; ++i) { scanf("%s", in[i]); } for (int i = 0; i < 3; ++i) { int tam = strlen(in[i]); for (int j = 0; j < tam; ++j) { cnt[i][in[i][j] - 'a']++; } } int maior = 0; int qnttmp = 0; int qnt2tmp = 0; int tam = strlen(in[0]); bool ok = true; for (int i = 0; i <= tam; ++i) { int qnt = 0x3f3f3f3f; for (int j = 0; j < 27; ++j) { needed[j] = cnt[1][j] * i; if (needed[j] > 0) { if (cnt[0][j] >= needed[j]) { qnt = min(qnt, i); lef[j] = cnt[0][j] - needed[j]; } else { ok = false; break; } } else { lef[j] = cnt[0][j]; } } if (!ok) break; int qnt2 = 0x3f3f3f3f; for (int j = 0; j < 27; ++j) { if (cnt[2][j] > 0) { qnt2 = min(qnt2, lef[j] / cnt[2][j]); } } int ans = 0; if (qnt != 0x3f3f3f3f) { ans = qnt; } if (qnt2 != 0x3f3f3f3f) { ans += qnt2; } if (ans > maior) { maior = ans; if (qnt != 0x3f3f3f3f) qnttmp = qnt; else qnttmp = 0; if (qnt2 != 0x3f3f3f3f) qnt2tmp = qnt2; else qnt2tmp = 0; } } for (int i = 0; i < qnttmp; ++i) { printf("%s", in[1]); int tam = strlen(in[1]); for (int j = 0; j < tam; ++j) { cnt[0][in[1][j] - 'a']--; } } for (int i = 0; i < qnt2tmp; ++i) { printf("%s", in[2]); int tam = strlen(in[2]); for (int j = 0; j < tam; ++j) { cnt[0][in[2][j] - 'a']--; } } for (int i = 0; i < 27; ++i) { while (cnt[0][i] > 0) { printf("%c", (char)(i + 'a')); cnt[0][i]--; } } printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 10; const long long INF = 4e18 + 10; const int maxn = 1 * 1e5 + 5; const int maxe = maxn; char a[maxn], b[maxn], c[maxn]; int cnt[300]; int need[2][300]; int MAIN() { scanf("%s%s%s", a, b, c); for (int i = 0; a[i]; ++i) ++cnt[a[i]]; for (int i = 0; b[i]; ++i) need[0][b[i]]++; for (int i = 0; c[i]; ++i) need[1][c[i]]++; int bmx = inf; for (int i = 0; i < 300; ++i) { if (!need[0][i]) continue; bmx = min(bmx, cnt[i] / need[0][i]); } int mx = -1, rb, rc; for (int bcnt = 0; bcnt <= bmx; ++bcnt) { int ccnt = inf; for (int i = 0; i < 300; ++i) { if (!need[1][i]) continue; int res = cnt[i] - bcnt * need[0][i]; if (res < need[1][i]) { ccnt = 0; break; } else { ccnt = min(ccnt, res / need[1][i]); } } if (bcnt + ccnt > mx) { mx = bcnt + ccnt; rb = bcnt, rc = ccnt; } } for (int i = 0; i < rb; ++i) { for (int j = 0; b[j]; ++j) { putchar(b[j]); --cnt[b[j]]; } } for (int i = 0; i < rc; ++i) { for (int j = 0; c[j]; ++j) { putchar(c[j]); --cnt[c[j]]; } } for (int i = 0; i < 300; ++i) while (cnt[i]--) putchar(i); puts(""); return 0; } int main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(16); int ret = MAIN(); return ret; }
#include <bits/stdc++.h> using namespace std; const int MaxN = 200009; char a[MaxN], b[MaxN], c[MaxN], k[MaxN]; vector<int> cnta, cntb, cntc; int main() { cnta.resize(30); cntb.resize(30); cntc.resize(30); gets(a); gets(b); gets(c); int alen = strlen(a); int blen = strlen(b); int clen = strlen(c); for (int i = 0; i < alen; i++) { cnta[a[i] - 'a']++; } for (int i = 0; i < blen; i++) { cntb[b[i] - 'a']++; } for (int i = 0; i < clen; i++) { cntc[c[i] - 'a']++; } string bstr = b, cstr = c; string ans; while (true) { int bm = INT_MAX, cm = INT_MAX; for (int i = 0; i < 26; i++) { if (cntb[i] != 0) bm = min(cnta[i] / cntb[i], bm); if (cntc[i] != 0) cm = min(cnta[i] / cntc[i], cm); } if (bm <= 0 && cm <= 0) break; if (bm >= cm) { ans += bstr; for (int i = 0; i < 26; i++) { cnta[i] -= cntb[i]; } } else { ans += cstr; for (int i = 0; i < 26; i++) { cnta[i] -= cntc[i]; } } } for (int i = 0; i < 26; i++) { for (int j = 0; j < cnta[i]; j++) ans += 'a' + i; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const int Sz = 111011; string a, b, c; int cnt[10][30]; int k = 0, l = 0; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> a >> b >> c; for (int i = 0; i < a.size(); i++) cnt[1][a[i]]++; for (int i = 0; i < b.size(); i++) cnt[2][b[i]]++; for (int i = 0; i < c.size(); i++) cnt[3][c[i]]++; for (int x = 0; x <= a.size(); x++) { int y = 100000; for (int i = 'a'; i <= 'z'; i++) { if (cnt[1][i] < cnt[2][i] * x) { goto end; } } for (int i = 'a'; i <= 'z'; i++) { if (cnt[3][i]) { y = min((cnt[1][i] - cnt[2][i] * x) / cnt[3][i], y); } } if (y == 100000) y = 0; if (k + l < x + y) { k = x; l = y; } } end:; for (int i = 1; i <= k; i++) cout << b; for (int i = 1; i <= l; i++) cout << c; for (int i = 'a'; i <= 'z'; i++) { for (int j = 1; j <= (cnt[1][i] - cnt[2][i] * k - cnt[3][i] * l); j++) cout << char(i); } return 0; }
#include <bits/stdc++.h> using namespace std; char a[100010], b[100010], c[100010]; int cnt_a[26], cnt_b[26], cnt_c[26]; int remain[26], len_a, len_b, len_c; int c_num, max_b, max_c; void subtract(int b_num) { if (b_num * len_b > len_a) return; for (int i = 0; i < 26; i++) { remain[i] = cnt_a[i] - b_num * cnt_b[i]; if (remain[i] < 0) return; } int c_num = 200000; for (int i = 0; i < 26; i++) if (cnt_c[i] != 0) if (remain[i] / cnt_c[i] < c_num) c_num = remain[i] / cnt_c[i]; if (c_num != 200000 && b_num + c_num > max_b + max_c) max_b = b_num, max_c = c_num; subtract(b_num + 1); } int main() { while (fgets(a, sizeof(a), stdin) != NULL) { fgets(b, sizeof(b), stdin); fgets(c, sizeof(c), stdin); memset(cnt_a, 0, sizeof(cnt_a)); memset(cnt_b, 0, sizeof(cnt_b)); memset(cnt_c, 0, sizeof(cnt_c)); len_a = strlen(a), len_b = strlen(b), len_c = strlen(c); a[len_a - 1] = 0, b[len_b - 1] = 0, c[len_c - 1] = 0; len_a--, len_b--, len_c--; for (int i = 0; i < len_a; i++) cnt_a[a[i] - 'a']++; for (int i = 0; i < len_b; i++) cnt_b[b[i] - 'a']++; for (int i = 0; i < len_c; i++) cnt_c[c[i] - 'a']++; max_b = 0, max_c = 0; subtract(0); for (int i = 1; i <= max_b; i++) printf("%s", b); for (int i = 1; i <= max_c; i++) printf("%s", c); for (int i = 0; i < 26; i++) { for (int j = 1; j <= cnt_a[i] - max_b * cnt_b[i] - max_c * cnt_c[i]; j++) printf("%c", i + 'a'); } printf("\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string S, A, B; int s[27] = {0}, a[27] = {0}, b[27] = {0}; cin >> S >> A >> B; for (auto it : S) s[it - 'a']++; for (auto it : A) a[it - 'a']++; for (auto it : B) b[it - 'a']++; int foo = INT_MAX, bar = 0; for (int i = 0; i < 26; i++) if (a[i]) foo = min(foo, s[i] / a[i]); int T = foo; for (int i = 0; i <= T; i++) { int temp = INT_MAX; for (int j = 0; j < 26; j++) if (b[j]) temp = min(temp, (s[j] - a[j] * i) / b[j]); if (i + temp > foo + bar) foo = i, bar = temp; } for (int i = 0; i < foo; i++) cout << A; for (int i = 0; i < 26; i++) s[i] -= foo * a[i]; for (int i = 0; i < bar; i++) cout << B; for (int i = 0; i < 26; i++) s[i] -= bar * b[i]; for (int i = 0; i < 26; i++) for (int j = 0; j < s[i]; j++) cout << (char)(i + 'a'); cout << endl; }
#include <bits/stdc++.h> using namespace std; long long a[200005], b[200005], f[100005], dp[100005]; int main() { string a, b, c; long long p, cnt[26] = {0}, cnt1[26] = {0}, cnt2[26] = {0}, i, j, k, l, m, n; cin >> a >> b >> c; for (i = 0; i < a.size(); i++) cnt[a[i] - 'a']++; for (i = 0; i < b.size(); i++) cnt1[b[i] - 'a']++; for (i = 0; i < c.size(); i++) cnt2[c[i] - 'a']++; k = 1e9 + 7; l = 1e9 + 7; for (i = 0; i < 26; i++) if (cnt1[i] != 0) k = min(k, cnt[i] / cnt1[i]); for (i = 0; i < 26; i++) if (cnt2[i] != 0) l = min(l, cnt[i] / cnt2[i]); long long ans = 0; long long o = 0; for (i = 0; i <= k; i++) { l = 1e9 + 7; for (j = 0; j < 26; j++) { if (cnt2[j] > 0) l = min(l, (cnt[j] - i * cnt1[j]) / cnt2[j]); } if ((l + i) > ans) ans = l + i, o = i; } for (i = 1; i <= o; i++) cout << b; for (i = 0; i < ans - o; i++) cout << c; for (i = 0; i < 26; i++) { p = cnt[i] - o * cnt1[i] - (ans - o) * cnt2[i]; if (p > 0) { for (j = 0; j < p; j++) cout << char('a' + i); } } }
#include <bits/stdc++.h> using namespace std; long long frq[3][26]; string a, b, c; int F, S; bool Fe, Se; bool Ex(int id) { for (int i = 0; i < 26; i++) { if (frq[0][i] < frq[id][i]) { return false; } } return true; } void del(int id, int num) { for (int i = 0; i < 26; i++) { frq[0][i] -= (frq[id][i] * num); } } bool can1(int num, int id) { for (int i = 0; i < 26; i++) { if (frq[0][i] < frq[id][i] * num) return false; } return true; } bool can2(int num1, int num) { for (int i = 0; i < 26; i++) { if (frq[0][i] - (frq[1][i] * num1) < frq[2][i] * num) return false; } return true; } int main() { cin >> a >> b >> c; for (int i = 0; i < (int)a.size(); i++) frq[0][(int)a[i] - 97]++; for (int i = 0; i < (int)b.size(); i++) frq[1][(int)b[i] - 97]++; for (int i = 0; i < (int)c.size(); i++) frq[2][(int)c[i] - 97]++; Fe = Ex(1); Se = Ex(2); if (Fe && Se) { int t1, t2; t1 = t2 = 0; for (int i = 0; i <= 1e5; i++) { if (can1(i, 1)) { t1 = i; } int l = 0, r = 1e5, m; while (l <= r) { m = (l + r) >> 1; if (can2(t1, m)) { t2 = m; l = m + 1; } else r = m - 1; } if (t1 + t2 > F + S) { F = t1; S = t2; } t1 = t2 = 0; } del(1, F); del(2, S); for (int i = 0; i < F; i++) printf("%s", b.c_str()); for (int i = 0; i < S; i++) printf("%s", c.c_str()); } else if (Fe) { int l = 0, r = 1e5, m, j = 0; while (l <= r) { m = (l + r) >> 1; if (can1(m, 1)) { j = m; l = m + 1; } else r = m - 1; } del(1, j); while (j--) { printf("%s", b.c_str()); } } else if (Se) { int l = 0, r = 1e5, m, j = 0; while (l <= r) { m = (l + r) >> 1; if (can1(m, 2)) { j = m; l = m + 1; } else r = m - 1; } del(2, j); while (j--) { printf("%s", c.c_str()); } } for (int i = 0; i < 26; i++) if (frq[0][i] > 0) { a = ""; while (frq[0][i]--) a += char(i + 97); printf("%s", a.c_str()); } return 0; }
#include <bits/stdc++.h> using namespace std; int main(int argc, char const* argv[]) { string a, b, c; cin >> a >> b >> c; int count[26], bcount[26], ccount[26]; for (int i = 0; i < 26; i += 1) { count[i] = 0; bcount[i] = 0; ccount[i] = 0; } for (int i = 0; i < a.length(); i += 1) { count[a[i] - 'a']++; } int tmp[26]; for (int i = 0; i < 26; i += 1) { tmp[i] = count[i]; } for (int i = 0; i < b.length(); i += 1) { bcount[b[i] - 'a']++; } for (int i = 0; i < c.length(); i += 1) { ccount[c[i] - 'a']++; } int match = 0; int r = 0, s = 0; for (int i = 0;; i += 1) { int x = a.length(); for (int j = 0; j < 26; j += 1) { if (bcount[j]) { x = min(x, count[j] / bcount[j]); } } if (x + i > match) { match = x + i; r = x; s = i; } bool flag = false; for (int j = 0; j < 26; j += 1) { if (count[j] < ccount[j]) { flag = true; break; } else { count[j] -= ccount[j]; } } if (flag) { break; } } string k; for (int i = 0; i < r; i += 1) { for (int j = 0; j < b.length(); j += 1) { k += b[j]; } } for (int i = 0; i < s; i += 1) { for (int j = 0; j < c.length(); j += 1) { k += c[j]; } } for (int i = 0; i < 26; i += 1) { tmp[i] = tmp[i] - bcount[i] * r - ccount[i] * s; } for (int i = 0; i < 26; i += 1) { while (tmp[i] > 0) { char x = i + 'a'; k += x; tmp[i]--; } } cout << k << endl; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse4") #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") using namespace std; const double pi = acos(-1.0); const int mod = 1000000007; const int inf = 0x3f3f3f3f; const int maxn = 200005; void task() { string a, b, c; cin >> a >> b >> c; std::map<int, int> A, B, C; for (char c : a) A[c - 'a']++; for (char c : b) B[c - 'a']++; for (char c : c) C[c - 'a']++; int mxB = inf, mxC = inf; for (int i = 0; i < 26; i++) { if (B[i]) mxB = min(mxB, A[i] / B[i]); if (C[i]) mxC = min(mxC, A[i] / C[i]); } int best = -1; int k1, k2; for (int k = 0; k < ((int)(a).size()); k++) { bool f = 1; int mn = inf; for (int i = 0; i < 26; i++) { if (B[i] * k > A[i]) { f = 0; } } if (!f) break; for (int i = 0; i < 26; i++) { if (C[i]) mn = min(mn, (A[i] - B[i] * k) / C[i]); } if (best < mn + k) best = mn + k, k1 = k, k2 = mn; } string ans; for (int i = 0; i < k1; i++) { ans += b; } for (int i = 0; i < k2; i++) { ans += c; } for (int i = 0; i < 26; i++) { A[i] -= B[i] * k1 + C[i] * k2; for (int j = 0; j < A[i]; j++) { ans += char(i + 'a'); } } cout << ans << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); int T = 1; while (T--) { task(); } return 0; }
#include <bits/stdc++.h> using namespace std; void _551A() { int n; cin >> n; int* array = new int[n]; int* count_arr = new int[2001]; for (int i = 0; i < 2001; i++) { count_arr[i] = 0; } int tmp; for (int i = 0; i < n; i++) { cin >> tmp; array[i] = tmp; count_arr[tmp]++; } int cnt = 1; tmp = 1; int prev = 1; for (int i = 2001; i >= 0; i--) { if (count_arr[i] == 0) continue; tmp = count_arr[i]; cnt = prev + cnt - 1; count_arr[i] = cnt; prev = tmp; cnt++; } for (int i = 0; i < n; i++) { cout << count_arr[array[i]] << " "; } } void print_bucket(int* bucket) { for (int i = 0; i < 26; i++) cout << char(i + 'a') << ":" << bucket[i] << " "; cout << endl; } bool has_enough_letters(int* bucket, int* sub) { int bucket_copy[26]; for (int i = 0; i < 26; i++) bucket_copy[i] = bucket[i]; for (int i = 0; i < 26; i++) { if ((bucket_copy[i] -= sub[i]) < 0) { return false; } } return true; } int runA(string& final_string, string& a, string& b, string& c) { int bucket[26]; int bucket_b[26]; int bucket_c[26]; int bucket_c_a[26]; for (int i = 0; i < 26; i++) { bucket[i] = 0; bucket_b[i] = 0; bucket_c[i] = 0; } for (int i = 0; i < a.size(); i++) { bucket[a[i] - 'a']++; } for (int i = 0; i < b.size(); i++) { bucket_b[b[i] - 'a']++; } for (int i = 0; i < c.size(); i++) { bucket_c[c[i] - 'a']++; } int total = 0; int max_c = 0; int max_b = 0; for (int i = 0;; i++) { if (has_enough_letters(bucket, bucket_b) or i == 0) { for (int j = 0; j < 26 and i != 0; j++) { bucket[j] -= bucket_b[j]; } int local_max_c = 0; for (int k = 0; k < 26; k++) bucket_c_a[k] = bucket[k]; for (local_max_c = 0; has_enough_letters(bucket_c_a, bucket_c);) { for (int j = 0; j < 26; j++) bucket_c_a[j] -= bucket_c[j]; local_max_c++; } if (local_max_c + i >= max_c + max_b) { max_b = i; max_c = local_max_c; } } else break; } for (int i = 0; i < 26; i++) { bucket[i] = 0; } for (int i = 0; i < a.size(); i++) { bucket[a[i] - 'a']++; } for (int i = 0; i < b.size(); i++) { bucket[b[i] - 'a'] -= max_b; } for (int i = 0; i < c.size(); i++) { bucket[c[i] - 'a'] -= max_c; } for (int i = 0; i < max_b; i++) { cout << b; } for (int i = 0; i < max_c; i++) { cout << c; } for (int i = 0; i < 26; i++) for (int j = 0; j < bucket[i]; j++) cout << char('a' + i); cout << endl; } bool enough_letters(int* bucket, int* bucket_sub, int size) { for (int i = 0; i < 26; i++) if (bucket[i] - (bucket_sub[i] * size) < 0) return false; return true; } void _551B() { string a, b, c; cin >> a >> b >> c; string final_string_a = "", final_string_b; int bucket[26]; int bucket_b[26]; int bucket_c[26]; for (int i = 0; i < 26; i++) { bucket[i] = 0; bucket_b[i] = 0; bucket_c[i] = 0; } for (int i = 0; i < a.size(); i++) { bucket[a[i] - 'a']++; } for (int i = 0; i < b.size(); i++) { bucket_b[b[i] - 'a']++; } for (int i = 0; i < c.size(); i++) { bucket_c[c[i] - 'a']++; } int x, y; int max = -5, max_x = -1, max_y = -1; bool test; for (x = 0;; x++) { if (!enough_letters(bucket, bucket_b, x) and x != 0) { break; } int local_max_c = 2000000000; for (int i = 0; i < 26; i++) { if (bucket_c[i] && (bucket[i] - bucket_b[i] * x) / bucket_c[i] < local_max_c) { local_max_c = (bucket[i] - bucket_b[i] * x) / bucket_c[i]; } } if (local_max_c + x > max) { max = local_max_c + x; max_x = x; max_y = local_max_c; } } string final; for (int i = 0; i < max_x; i++) { final.append(b); } for (int i = 0; i < max_y; i++) { final.append(c); } for (int i = 0; i < 26; i++) { for (int j = 0; j < bucket[i] - bucket_b[i] * max_x - bucket_c[i] * max_y; j++) final.push_back('a' + i); } cout << final << endl; } void _552A() { int n; cin >> n; int cnt = 0; int x1, x2, y1, y2; for (int i = 0; i < n; i++) { cin >> x1 >> y1 >> x2 >> y2; int x, y; x = x2 - x1 + 1; y = y2 - y1 + 1; cnt += x * y; } cout << cnt; } int pow(int x, int e) { if (e == 0) return 1; int acc = x; for (int i = 0; i < e - 1; i++) { acc *= x; } return acc; } void _552B() { long long cnt = 0; long long n; cin >> n; if (n < 10) { cout << n << endl; return; } if (n == 1000000000) { cout << 8888888899 << endl; return; } for (long long i = 1; i <= 9; i++) { if (pow(10, i) <= n) { long long c = pow(10, i) * i * 0.9; cnt += c; } else { long long c = ((n - pow(10, i - 1)) + 1) * i; cnt += c; break; } } cout << cnt << endl; } int main(int argc, char** argv) { _551B(); return 0; }
#include <bits/stdc++.h> using namespace std; char s[4][100005]; int n[5], p[4][27], ans, vans, vf, tans; int main() { int i, j, m; for (i = 1; i <= 3; i++) { scanf("%s", s[i] + 1); n[i] = strlen(s[i] + 1); } for (i = 1; i <= 3; i++) { for (j = 1; j <= n[i]; j++) { p[i][s[i][j] - 'a']++; } } for (i = 0; i <= 100000; i++) { vans = 100000; for (j = 0; j < 26; j++) { if ((p[1][j] - p[2][j] * i) < 0) { vf = 1; break; } if (p[3][j] != 0) vans = min(vans, (p[1][j] - p[2][j] * i) / p[3][j]); } if (vf) break; if (ans < vans + i) { ans = vans + i; tans = i; } } for (i = 1; i <= tans; i++) { printf("%s", s[2] + 1); } for (i = 1; i <= ans - tans; i++) { printf("%s", s[3] + 1); } for (i = 0; i < 26; i++) { p[1][i] -= tans * p[2][i] + (ans - tans) * p[3][i]; } for (i = 0; i < 26; i++) { while (p[1][i]) { printf("%c", i + 'a'); p[1][i]--; } } printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; long long ac[256]; long long bc[256]; long long cc[256]; long long ta[256]; long long cb, ck, answ; char b[100001], c[100001]; long long inc(long long *const p, const char *s) { long long i = 0; while (s[i]) ++p[s[i++]]; return i; } long long test_c(void) { long long m = 1000000000000007; for (register long long i = 0; i < 256; ++i) if (cc[i]) m = min(ac[i] / cc[i], m); return m; } void save_a(void) { memcpy(ta, ac, sizeof(ac)); } void restor(void) { memcpy(ac, ta, sizeof(ac)); } bool dec(void) { for (register long long i = 0; i < 256; ++i) if (ac[i] < bc[i]) return false; for (register long long i = 0; i < 256; ++i) ac[i] -= bc[i]; return true; } void dec_c(void) { for (register long long i = 0; i < 256; ++i) ac[i] -= cc[i] * ck; } int main() { scanf("%s", b); long long ln = inc(ac, b); scanf("%s", b); scanf("%s", c); inc(bc, b); inc(cc, c); for (register long long i = 0; i <= ln; ++i) { long long cck = test_c(); if (answ < i + cck) answ = i + cck, cb = i, ck = cck, save_a(); if (!dec()) break; } if (answ) restor(); dec_c(); for (register long long i = 0; i < cb; ++i) printf("%s", b); for (register long long i = 0; i < ck; ++i) printf("%s", c); for (register long long i = 0; i < 256; ++i) for (register long long j = 0; j < ac[i]; ++j) putc(i, stdout); putc('\n', stdout); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100500; int n, m, k, A[30], B[30], AB[30]; int d, res, fst, sec; char s[N], a[N], b[N], ans[N]; int main() { scanf("%s %s %s", s + 1, a + 1, b + 1); n = strlen(s + 1); m = strlen(a + 1); k = strlen(b + 1); for (int i = 1; i <= n; i++) AB[s[i] - 'a']++; for (int i = 1; i <= m; i++) A[a[i] - 'a']++; for (int i = 1; i <= k; i++) B[b[i] - 'a']++; for (int i = 0;; i++) { int x = n; for (int j = 0; j < 26; j++) if (B[j]) x = min(x, AB[j] / B[j]); if (x + i > res) { res = x + i; fst = i; sec = x; } bool f = false; for (int j = 0; j < 26; j++) if (AB[j] < A[j]) { f = true; break; } else AB[j] -= A[j]; if (f) break; } for (int i = 1; i <= fst; i++) for (int j = 1; j <= m; j++) d++, ans[d] = a[j]; for (int i = 1; i <= sec; i++) for (int j = 1; j <= k; j++) d++, ans[d] = b[j]; for (int i = 0; i < 26; i++) AB[i] = -A[i] * fst - B[i] * sec; for (int i = 1; i <= n; i++) AB[s[i] - 'a']++; for (int i = 0; i < 26; i++) for (int j = 1; j <= AB[i]; j++) ans[++d] = 'a' + i; for (int i = 1; i <= n; i++) printf("%c", ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; int ss[30] = {0}; int aa[30] = {0}; int bb[30] = {0}; inline int hudai(int z) { int cnt = INT_MAX; for (int i = 0; i < 26; i++) { int x = ss[i] - z * aa[i]; if (!bb[i]) continue; cnt = min(x / bb[i], cnt); } return cnt; } int main() { ios_base::sync_with_stdio(false); string s, a, b; cin >> s >> a >> b; for (int i = 0; i < s.size(); i++) { int x = s[i] - 'a'; ss[x]++; } for (int i = 0; i < a.size(); i++) { int x = a[i] - 'a'; aa[x]++; } for (int i = 0; i < b.size(); i++) { int x = b[i] - 'a'; bb[x]++; } int cnta = INT_MAX; for (int i = 0; i < 26; i++) { if (!aa[i]) continue; cnta = min(cnta, ss[i] / aa[i]); } int koyta_a = 0; int koyta_b = 0; int cnt = 0; for (int i = 0; i <= cnta; i++) { int tem = hudai(i); if (tem + i > cnt) { cnt = tem + i; koyta_b = tem; koyta_a = i; } } for (int i = 0; i < koyta_a; i++) cout << a; for (int i = 0; i < koyta_b; i++) cout << b; for (int i = 0; i < 26; i++) { int x = ss[i] - koyta_a * aa[i] - koyta_b * bb[i]; char c = i + 'a'; for (int j = 0; j < x; j++) cout << c; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}; int dy[] = {0, 1, 1, 1, 0, -1, -1, -1}; int di[] = {1, -1, 0, 0}; int dj[] = {0, 0, 1, -1}; void file() {} int main() { file(); string a, b, c; while (cin >> a >> b >> c) { int alen = a.length(); int blen = b.length(); int clen = c.length(); vector<int> cntA(26), cntB(26), cntC(26); for (int i = 0; i < alen; i++) { cntA[a[i] - 'a']++; } for (int i = 0; i < blen; i++) { cntB[b[i] - 'a']++; } for (int i = 0; i < clen; i++) { cntC[c[i] - 'a']++; } int cnt1 = 0, cnt2 = 0; for (int i = 0; i <= alen; i++) { int f = 1; for (int j = 0; j < 26; j++) { if (cntA[j] < i * cntB[j]) { f = 0; break; } } if (f == 0) { break; } int cmx = alen; for (int j = 0; j < 26; j++) { if (cntC[j]) { cmx = min(cmx, (cntA[j] - i * cntB[j]) / cntC[j]); } } if (cmx + i >= cnt1 + cnt2) { cnt1 = i; cnt2 = cmx; } } string s = ""; for (int i = 0; i < cnt1; i++) { s += b; } for (int i = 0; i < cnt2; i++) { s += c; } for (int i = 0; i < 26; i++) { for (int j = 0; j < cntA[i] - cntB[i] * cnt1 - cntC[i] * cnt2; j++) { s += char(i + 'a'); } } cout << s << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = (int)1e5 + 5; char s[N], a[N], b[N]; int counts[N], counta[N], countb[N]; int main() { scanf("%s %s %s", s, a, b); int slen = strlen(s); int alen = strlen(a); int blen = strlen(b); for (int i = 0; i < slen; ++i) ++counts[s[i] - 'a']; for (int i = 0; i < alen; ++i) ++counta[a[i] - 'a']; for (int i = 0; i < blen; ++i) ++countb[b[i] - 'a']; int bstrings = 0, astrings = 0, mxa = 0, mxb = 0, mx = 0, i = 0; while (1) { for (i = 0; i < 26; ++i) if (counts[i] < countb[i] * bstrings) break; if (i != 26) { break; } astrings = N + 5; for (i = 0; i < 26; ++i) if (counta[i]) astrings = min(astrings, (counts[i] - (bstrings * countb[i])) / counta[i]); if (astrings + bstrings > mx) { mx = astrings + bstrings; mxa = astrings; mxb = bstrings; } ++bstrings; } for (i = 0; i < mxa; ++i) for (int j = 0; j < alen; ++j) printf("%c", a[j]); for (i = 0; i < mxb; ++i) for (int j = 0; j < blen; ++j) printf("%c", b[j]); for (i = 0; i < 26; ++i) { int tmp = max(0, counts[i] - (countb[i] * mxb) - (counta[i] * mxa)); cout << string(tmp, 'a' + i); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int outa = 100005, outb = 100005, max; char a[100005], b[100005], c[100005]; scanf("%s %s %s", a, b, c); int counta[26] = {0}, countb[26] = {0}, countc[26] = {0}; for (int i = 0; a[i] != '\0'; ++i) counta[a[i] - 'a']++; for (int i = 0; b[i] != '\0'; ++i) countb[b[i] - 'a']++; for (int i = 0; c[i] != '\0'; ++i) countc[c[i] - 'a']++; for (int i = 0; i < 26; ++i) if (countb[i] > 0) outa = min(outa, counta[i] / countb[i]); for (int i = 0; i < 26; ++i) if (countc[i] > 0) outb = min(outb, (counta[i] - outa * countb[i]) / countc[i]); while (outa > 0) { int temp = 100005; for (int i = 0; i < 26; ++i) if (countc[i] > 0) temp = min(temp, (counta[i] - (outa - 1) * countb[i]) / countc[i]); if (temp >= outb + 1) { outb = temp; outa--; } else break; } for (int i = 0; i < 26; ++i) counta[i] = counta[i] - (outa * countb[i]) - (outb * countc[i]); while (outa > 0) { printf("%s", b); outa--; } while (outb > 0) { printf("%s", c); outb--; } for (int i = 0; i < 26; ++i) { while (counta[i] > 0) { printf("%c", 'a' + i); counta[i]--; } } printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 256; const char nxt = '\n'; inline void in(string Q) { Q += ".in"; freopen(Q.c_str(), "r", stdin); } inline void out(string Q) { Q += ".out"; freopen(Q.c_str(), "w", stdout); } inline void open_file(string Q) { in(Q); out(Q); } struct str { int a, b; str() { a = 0; b = 0; } str(int q, int w) { a = q; b = w; } inline bool check(int q, int w) { if (a + b < q + w) return true; else return false; } inline void change(int q, int w) { if (check(q, w)) a = q, b = w; } } can; string a, b, c; int A[MAXN], B[MAXN], C[MAXN]; inline void init() { cin >> a >> b >> c; for (auto to : a) A[to]++; for (auto to : b) B[to]++; for (auto to : c) C[to]++; } int main() { if (0) open_file(""); init(); for (int i = 0; i <= min(int(a.size()), 16000); ++i) { bool ok = 1; for (char x = 'a'; x <= 'z'; ++x) { if (B[x] * i > A[x]) { ok = false; break; } } if (!ok) continue; int canC = INT_MAX; for (char x = 'a'; x <= 'z'; ++x) { if (!C[x]) { continue; } canC = min(canC, (A[x] - (B[x] * i)) / C[x]); } if (canC == INT_MAX) canC = 0; can.change(i, canC); } for (char x = 'a'; x <= 'z'; ++x) { A[x] -= (B[x] * can.a); A[x] -= (C[x] * can.b); } for (int i = 0; i < can.a; ++i) { cout << b; } for (int i = 0; i < can.b; ++i) { cout << c; } for (char x = 'a'; x <= 'z'; ++x) { while (A[x]--) { cout << x; } } return 0; }
#include <bits/stdc++.h> using namespace std; string a, b, c, k; int d[26], p[26], q[26]; int times_b, times_c; inline void fill_ini() { for (int i = 0; i < a.size(); i++) { d[a[i] - 'a']++; } for (int i = 0; i < b.size(); i++) { p[b[i] - 'a']++; } for (int i = 0; i < c.size(); i++) { q[c[i] - 'a']++; } } inline int tb() { int minn = INT_MAX; for (int i = 0; i < 26; i++) { if (p[i] == 0) continue; minn = min(minn, d[i] / p[i]); } return minn; } inline int tc() { int minn = INT_MAX; for (int i = 0; i < 26; i++) { if (q[i] == 0) continue; minn = min(minn, d[i] / q[i]); } return minn; } inline void erase_b(int times) { for (int i = 0; i < 26; i++) { d[i] -= (p[i] * times); } } inline void erase_c(int times) { for (int i = 0; i < 26; i++) { d[i] -= (q[i] * times); } } inline void printer_b(int times) { for (int i = 0; i < times; i++) { cout << b; } } inline void printer_c(int times) { for (int i = 0; i < times; i++) { cout << c; } } inline void printer() { for (int i = 0; i < 26; i++) { for (int j = 0; j < d[i]; j++) { cout << (char)(i + 'a'); } } cout << '\n'; } inline void solver() { fill_ini(); times_b = tb(); times_c = tc(); while (true) { if (times_b <= 0 && times_c <= 0) break; if (times_b >= times_c && times_b > 0) { cout << b; erase_b(1); times_b = tb(); times_c = tc(); } else if (times_c > 0) { cout << c; erase_c(1); times_c = tc(); times_b = tb(); } } printer(); } inline void start() { cin >> a >> b >> c; memset(d, 0, sizeof(d)); memset(q, 0, sizeof(q)); memset(p, 0, sizeof(p)); solver(); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); start(); return 0; }
#include <bits/stdc++.h> using std::getchar; using std::putchar; int main() { int a[26] = {0}, b[26] = {0}, c[26] = {0}, len_b(0), len_c(0); char input, string_b[(int)1e+5], string_c[(int)1e+5]; while ((input = getchar()) != '\n') a[input - 'a']++; while ((input = getchar()) != '\n') { b[input - 'a']++; string_b[len_b++] = input; } while ((input = getchar()) != '\n') { c[input - 'a']++; string_c[len_c++] = input; } int b_max(0), c_now, b_now, ans(0), b_ans, c_ans; bool b_max_found(false), c_now_found(false); for (int i(0); i < 26; ++i) if (b[i] && (!b_max_found || a[i] / b[i] < b_max)) { b_max = a[i] / b[i]; b_max_found = true; } for (int b_now(0); b_now <= b_max; ++b_now) { c_now = 0; c_now_found = false; for (int i(0); i < 26; ++i) if (c[i] && (!c_now_found || (a[i] - b[i] * b_now) / c[i] < c_now)) { c_now = (a[i] - b_now * b[i]) / c[i]; c_now_found = true; } if (b_now + c_now > ans) { b_ans = b_now; c_ans = c_now; ans = b_ans + c_ans; } } for (int i(0); i < 26; ++i) { a[i] -= c_ans * c[i] + b_ans * b[i]; while (a[i]--) putchar(i + 'a'); } while (b_ans--) for (int i(0); i < len_b; ++i) putchar(string_b[i]); while (c_ans--) for (int i(0); i < len_c; ++i) putchar(string_c[i]); }
#include <bits/stdc++.h> using namespace std; int main() { string a, b, c; cin >> a >> b >> c; int cnt_a[26], cnt_b[26], cnt_c[26]; memset(cnt_a, 0, sizeof(cnt_a)); memset(cnt_b, 0, sizeof(cnt_b)); memset(cnt_c, 0, sizeof(cnt_c)); for (int i = 0; i < a.size(); ++i) { cnt_a[a[i] - 'a']++; } for (int i = 0; i < b.size(); ++i) { cnt_b[b[i] - 'a']++; } for (int i = 0; i < c.size(); ++i) { cnt_c[c[i] - 'a']++; } int cnt[26]; memset(cnt, 0, sizeof(cnt)); int num = 0; int x = 0, y = 0; while (1) { int cur = 1000000000; for (int i = 0; i < 26; ++i) { if (cnt_a[i] - cnt[i] < 0) { cur = -1; break; } if (cnt_c[i] == 0) { continue; } cur = min(cur, (cnt_a[i] - cnt[i]) / cnt_c[i]); } if (cur == -1) { break; } else { if (x + y < num + cur) { x = num; y = cur; } } num++; for (int i = 0; i < 26; ++i) { cnt[i] += cnt_b[i]; } } for (int i = 0; i < x; ++i) { cout << b; } for (int i = 0; i < y; ++i) { cout << c; } for (int i = 0; i < 26; ++i) { cnt_a[i] -= cnt_b[i] * x + cnt_c[i] * y; } for (int i = 0; i < 26; ++i) { for (int j = 0; j < cnt_a[i]; ++j) { cout << char(i + 'a'); } } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int a[100007], b[100007], c[100007]; string s, ss, sss; int min1 = 0x3f3f3f3f, min2 = 0x3f3f3f3f, m = 0, n = 0; int main() { memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); memset(c, 0, sizeof(c)); cin >> s >> ss >> sss; for (int i = 0; i < s.size(); i++) a[s[i] - 'a']++; for (int i = 0; i < ss.size(); i++) b[ss[i] - 'a']++; for (int i = 0; i < sss.size(); i++) c[sss[i] - 'a']++; for (int i = 0; i < 26; i++) if (b[i]) min1 = min(min1, a[i] / b[i]); for (int i = 0; i <= min1; i++) { for (int j = 0; j < 26; j++) { if (c[j]) { min2 = min(min2, (a[j] - i * b[j]) / c[j]); } } if (i + min2 > m + n) { m = i; n = min2; } } for (int i = 0; i < m; i++) { cout << ss; } for (int i = 0; i < n; i++) { cout << sss; } for (int i = 0; i < 26; i++) { if (a[i] - b[i] * m - c[i] * n > 0) { cout << char('a' + i); a[i]--; i--; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char a[100005], b[100005], c[100005]; long long int cnta[26] = {0}, i, j; long long int cntb[26] = {0}; long long int cntc[26] = {0}; cin >> a >> b >> c; long long int len1, len2, len3; len1 = strlen(a); len2 = strlen(b); len3 = strlen(c); for (i = 0; i < len1; i++) cnta[a[i] - 'a']++; for (i = 0; i < len2; i++) cntb[b[i] - 'a']++; for (i = 0; i < len3; i++) cntc[c[i] - 'a']++; long long int m = INT_MAX, n = INT_MAX; while (1) { for (i = 0; i < len2; i++) { m = min(m, cnta[b[i] - 'a'] / cntb[b[i] - 'a']); } for (i = 0; i < len3; i++) { n = min(n, cnta[c[i] - 'a'] / cntc[c[i] - 'a']); } if (m == 0 && n == 0) break; if (m > n) { for (i = 0; i < len2; i++) { printf("%c", b[i]); cnta[b[i] - 'a']--; } } else { for (i = 0; i < len3; i++) { printf("%c", c[i]); cnta[c[i] - 'a']--; } } } for (i = 0; i < 26; i++) { for (j = 0; j < cnta[i]; j++) printf("%c", i + 97); } return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/stack:336777216") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T> using vec3 = array<T, 3>; template <typename T> using vec2 = array<T, 2>; template <typename T, size_t _bits> using mat2d = array<array<T, _bits>, _bits>; inline namespace mylovelytemplate { ostream &operator<<(ostream &os, const stringstream &ss) { os << ss.str(); return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &d) { for (auto it = d.begin(); it != d.end(); it++) { os << *it << " "; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << " , " << p.second; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (int i = 0; i < v.size(); i++) { os << v[i] << " "; } return os; } template <typename T, size_t x> ostream &operator<<(ostream &os, const array<T, x> &v) { for (auto it = v.begin(); it != v.end(); it++) { os << *it << " "; } return os; } struct comp { bool strbylength(const string &a, const string &b) { return a.size() < b.size(); } template <typename T1, typename T2> bool pairbysecond(const pair<T1, T2> &first, const pair<T1, T2> &second) { if (first.second != second.second) return first.second < second.second; return first.first < second.first; } }; struct hashing { size_t operator()(pair<int, int> p) const { return std::hash<size_t>()((size_t(p.first) << 32) + p.second); } }; template <typename T> int getbit(T s, int i) { return (s >> i) & 1; } template <typename T> T onbit(T s, int i) { return s | (T(1) << i); } template <typename T> T offbit(T s, int i) { return s & (~(T(1) << i)); } class DSU { public: int cnt, max_size; vector<int> parent, size; stack<pair<pair<int, int>, pair<int, int>>> history; DSU(int N) { cnt = N; parent = vector<int>(N); size = vector<int>(N, 1); max_size = 1; for (int i = 0; i < N; i++) parent[i] = i; } int find(int p) { int root = p; while (root != parent[root]) root = parent[root]; while (p != root) { int newp = parent[p]; parent[p] = root; p = newp; } return root; } int find_r(int p) { int root = p; while (root != parent[root]) root = parent[root]; return root; } void merge(int x, int y) { int i = find(x); int j = find(y); if (i == j) return; if (size[i] < size[j]) { parent[i] = j, size[j] += size[i]; max_size = max(max_size, size[j]); } else { parent[j] = i, size[i] += size[j]; max_size = max(max_size, size[i]); } cnt--; } void merge_r(int x, int y) { int i = find_r(x); int j = find_r(y); if (i == j) return; history.push({{i, parent[i]}, {j, size[j]}}); parent[i] = j; size[j] += size[i]; max_size = max(max_size, size[j]); cnt--; } void revert() { while (!history.empty()) { parent[history.top().first.first] = history.top().first.second; size[history.top().second.first] = history.top().second.second; history.pop(); } } bool connected(int x, int y) { return find(x) == find(y); } }; template <typename T> T totient(T x) { T result = x; for (int i = 0; i * i <= x; i++) { if (x % i == 0) { while (x % i == 0) { x /= i; } result -= result / i; } } if (x > 1) { result -= result / x; } return result; } template <typename T> T gcd(T a, T b) { while (b != 0) { T t = b; b = a % b; a = t; } return a; } template <typename T> T binaryexpo(T base, T exp, const T mod) { T ans = 1; base = base % mod; while (exp > 0) { if (exp & 1) { ans = (ans * base) % mod; } exp >>= 1; base = (base * base) % mod; } return ans % mod; } template <typename T> T binaryexpo(T base, const T exp) { T ans = 1; while (exp > 0) { if (exp & 1) { ans *= base; } exp >>= 1; base *= base; } return ans; } template <typename T> T getdistance3dpointtoline(vec3<T> &A, vec3<T> &B, vec3<T> &C) { vec3<T> BA, BC; for (int i = 0; i < 3; i++) { BA[i] = A[i] - B[i]; BC[i] = C[i] - B[i]; } vec3<T> BAXBC; BAXBC[0] = BA[1] * BC[2] - BA[2] * BC[1]; BAXBC[1] = BA[2] * BC[0] - BA[0] * BC[2]; BAXBC[2] = BA[0] * BC[1] - BA[1] * BC[0]; T modUP = 0.0, modDOWN = 0.0; for (int i = 0; i < 3; i++) { modUP += BAXBC[i] * BAXBC[i]; modDOWN += BC[i] * BC[i]; } T distance = modUP / modDOWN; return distance; } template <typename T, size_t _bits> mat2d<T, _bits> matmul(mat2d<T, _bits> &A, mat2d<T, _bits> &B) { mat2d<T, _bits> C; for (int i = 0; i < _bits; i++) { for (int j = 0; j < _bits; j++) { C[i][j] = 0; for (int k = 0; k < _bits; k++) { C[i][j] += A[i][k] * B[k][j]; } } } return C; } template <typename T, size_t _bits> mat2d<T, _bits> matmul(mat2d<T, _bits> &A, mat2d<T, _bits> &B, const T M) { mat2d<T, _bits> C; for (int i = 0; i < _bits; i++) { for (int j = 0; j < _bits; j++) { C[i][j] = 0; for (int k = 0; k < _bits; k++) { C[i][j] += (A[i][k] * B[k][j]) % M; } C[i][j] %= M; } } return C; } template <typename T, size_t _bits> mat2d<T, _bits> matrixexpo(mat2d<T, _bits> &X, ll n) { mat2d<T, _bits> C = {}; for (int i = 0; i < _bits; i++) C[i][i] = 1; while (n > 0) { if (n & 1) C = matmul(C, X); n >>= 1; X = matmul(X, X); } return C; } template <typename T, size_t _bits> mat2d<T, _bits> matrixexpo(mat2d<T, _bits> &X, ll n, const T M) { mat2d<T, _bits> C = {}; for (int i = 0; i < _bits; i++) C[i][i] = 1; while (n > 0) { if (n & 1) C = matmul(C, X, M); n >>= 1; X = matmul(X, X, M); } return C; } } // namespace mylovelytemplate string a, b, c; unordered_map<char, int> fa, fb, fc; int numbb() { int ma = 1e9; for (auto it = fb.begin(); it != fb.end(); it++) { if (fa.find(it->first) == fa.end()) { ma = 0; break; } int pos = fa[it->first] / it->second; ma = min(ma, pos); } return ma; } void useb(int times) { for (auto it = fb.begin(); it != fb.end(); it++) { fa[it->first] -= (times * it->second); } } void usec(int times) { for (auto it = fc.begin(); it != fc.end(); it++) { fa[it->first] -= (times * it->second); } } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> a >> b >> c; for (int i = 0; i < a.size(); i++) { fa[a[i]]++; } for (int i = 0; i < b.size(); i++) { fb[b[i]]++; } for (int i = 0; i < c.size(); i++) { fc[c[i]]++; } int bestb = 0, bestc = 0; int numb = numbb(); for (int i = 0; i <= numb; i++) { int numc = 1e9; for (auto it = fc.begin(); it != fc.end(); it++) { numc = min(numc, (fa[it->first] - fb[it->first] * i) / it->second); } if (i + numc > bestc + bestb) { bestb = i; bestc = numc; } } for (int i = 0; i < bestb; i++) cout << b; for (int i = 0; i < bestc; i++) cout << c; useb(bestb); usec(bestc); for (auto it = fa.begin(); it != fa.end(); it++) { for (int i = 0; i < it->second; i++) { cout << it->first; } } cout << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; struct q { int v, i; } qq[100005]; bool operator<(const q& a, const q& b) { return a.v > b.v; } char x[3][100005]; int cnt[3][26]; int i; bool ok; int aa, bb; int main() { for (int a = 0; a < 3; a++) { scanf("%s", x[a]); i = 0; while (x[a][i] != 0) { cnt[a][x[a][i] - 'a']++; i++; } } i = 0; for (int a = 0; a < 100005; a++) { ok = 1; i = INT_MAX; for (int b = 0; b < 26; b++) { if (cnt[0][b] - cnt[1][b] * a < 0) { ok = 0; break; } else if (cnt[2][b] != 0) i = min(i, (cnt[0][b] - cnt[1][b] * a) / cnt[2][b]); } if (ok) { if (aa + bb < a + i) { aa = a; bb = i; } } else break; } for (int a = 0; a < 26; a++) { cnt[0][a] -= cnt[1][a] * aa + cnt[2][a] * bb; while (cnt[0][a]-- > 0) printf("%c", 'a' + a); } while (aa-- > 0) printf("%s", x[1]); while (bb-- > 0) printf("%s", x[2]); printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; int a1[30] = {0}, a2[30] = {0}, a3[30] = {0}; int comp(int x) { int i; int m = 1000000000; for (i = 0; i < 26; i++) { if (a3[i]) m = min(m, (a1[i] - x * a2[i]) / a3[i]); } return m; } int main() { char a[100003], b[100003], c[100003]; int i, j, k = -1, x, y, z, p, q; scanf("%s", a); scanf("%s", b); scanf("%s", c); for (i = 0; a[i] != '\0'; i++) a1[a[i] - 'a']++; for (i = 0; b[i] != '\0'; i++) a2[b[i] - 'a']++; for (i = 0; c[i] != '\0'; i++) a3[c[i] - 'a']++; x = 0; while (1) { int flag = 0; for (i = 0; i < 26; i++) if (a1[i] - x * a2[i] < 0) { flag = 1; break; } if (flag == 1) break; j = comp(x); if (j < 0) break; if (k < x + j) { k = x + j; p = x; q = j; } x++; } for (i = 0; i < p; i++) cout << b; for (i = 0; i < q; i++) cout << c; for (i = 0; i < 26; i++) { a1[i] = a1[i] - p * a2[i] - q * a3[i]; for (j = 0; j < a1[i]; j++) printf("%c", i + 'a'); } cout << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const int alpha = 26; int main() { string a, b, c; cin >> a; cin >> b; cin >> c; int aa[alpha] = {0}, bb[alpha] = {0}, cc[alpha] = {0}; for (int i = 0; i < a.size(); i++) aa[a[i] - 'a']++; for (int i = 0; i < b.size(); i++) bb[b[i] - 'a']++; for (int i = 0; i < c.size(); i++) cc[c[i] - 'a']++; int k = 1000000000; for (int i = 0; i < alpha; i++) if (bb[i] != 0) k = min(k, aa[i] / bb[i]); int maxb = 0, maxc = 0; for (int i = k; i >= 0; i--) { int l = 1000000000; for (int j = 0; j < alpha; j++) if (cc[j] != 0) l = min(l, (aa[j] - bb[j] * i) / cc[j]); if (i + l > maxb + maxc) { maxb = i; maxc = l; } } for (int i = 0; i < maxb; i++) cout << b; for (int i = 0; i < maxc; i++) cout << c; for (int i = 0; i < alpha; i++) { aa[i] -= maxb * bb[i] + maxc * cc[i]; for (int j = 0; j < aa[i]; j++) cout << char(i + 'a'); } }
#include <bits/stdc++.h> using namespace std; template <typename T> void nexts(T *, int); template <typename T> void dump(T *arr, int cnt, char sep = ' '); void __init__(bool, bool); const int MAXMAXMAX = 1000 * 1000 * 1000; string a, b, c; char ans[100015]; vector<int> cnt_a(26); vector<int> cnt_b(26); vector<int> cnt_c(26); int main() { __init__(1, 0); cin >> a >> b >> c; for (int(i) = (0); (i) < (a.size()); ++(i)) ++cnt_a[a[i] - 'a']; for (int(i) = (0); (i) < (b.size()); ++(i)) ++cnt_b[b[i] - 'a']; for (int(i) = (0); (i) < (c.size()); ++(i)) ++cnt_c[c[i] - 'a']; int b_max = MAXMAXMAX; for (int(i) = (0); (i) < (26); ++(i)) if (cnt_b[i] != 0) b_max = min(b_max, cnt_a[i] / cnt_b[i]); int max_ans = 0; int max_b = 0; int max_c = 0; if (b_max == MAXMAXMAX) b_max = 0; vector<int> tmp = cnt_a; for (int(i) = (0); (i) < (b_max + 1); ++(i)) { if (i != 0) for (int(i) = (0); (i) < (26); ++(i)) tmp[i] -= cnt_b[i]; int c_max = MAXMAXMAX; for (int(i) = (0); (i) < (26); ++(i)) if (cnt_c[i] != 0) c_max = min(c_max, tmp[i] / cnt_c[i]); if (c_max == MAXMAXMAX) b_max = 0; if (i + c_max > max_ans) { max_ans = i + c_max; max_b = i; max_c = c_max; } } for (int(i) = (0); (i) < (max_b); ++(i)) cout << b; for (int(i) = (0); (i) < (max_c); ++(i)) cout << c; for (int(i) = (0); (i) < (26); ++(i)) { cnt_a[i] -= max_b * cnt_b[i] + max_c * cnt_c[i]; for (int(j) = (0); (j) < (cnt_a[i]); ++(j)) cout << (char)('a' + i); } cout << '\n'; return 0; } template <typename T> void nexts(T *arr, int cnt) { for (int i = 0; i < cnt; ++i) cin >> arr[i]; } template <typename T> void dump(T *arr, int cnt, char sep) { for (int i = 0; i < cnt; ++i) cout << arr[i] << sep; } void __init__(bool fastIOstream, bool acmp) { if (fastIOstream) { ios_base::sync_with_stdio(false); cin.tie(0); } if (acmp) { freopen("INPUT.TXT", "rt", stdin); freopen("OUTPUT.TXT", "wt", stdout); } }
#include <bits/stdc++.h> using namespace std; int au[500], bu[500], K, solc, solb, cu[500]; char a[500900], b[500900], c[500900]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> (a + 1) >> (b + 1) >> (c + 1); int A = strlen(a + 1); int B = strlen(b + 1); int C = strlen(c + 1); for (int i = 1; i <= B; ++i) bu[b[i]]++; for (int i = 1; i <= C; ++i) cu[c[i]]++; for (int i = 1; i <= A; ++i) au[a[i]]++; for (int i = 0; i <= A; ++i) { for (int j = 'a'; j <= 'z'; ++j) { au[j] -= i * bu[j]; if (au[j] < 0) { K = 1; for (int k = 'a'; k <= j; ++k) au[k] += i * bu[k]; break; } } if (K) break; int cnt = A; for (int j = 'a'; j <= 'z'; ++j) { if (cu[j]) cnt = min(au[j] / cu[j], cnt); } if (i + cnt > solb + solc) { solb = i; solc = cnt; } for (int j = 'a'; j <= 'z'; ++j) au[j] += i * bu[j]; } for (int i = 1; i <= solb; ++i) cout << (b + 1); for (int i = 1; i <= solc; ++i) cout << (c + 1); for (int i = 'a'; i <= 'z'; ++i) { au[i] -= bu[i] * solb + cu[i] * solc; while (au[i]--) cout << char(i); } return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; const int INF = 0x7f7f7f7f; int ca[30], cb[30], cc[30]; void calc(string s, int *arr) { for (int i = 0; s[i]; i++) arr[s[i] - 'a']++; } int main() { std::ios_base::sync_with_stdio(false); string a, b, c; cin >> a >> b >> c; calc(a, ca); calc(b, cb); calc(c, cc); int ansb, ansc; ansb = ansc = 0; for (int cntb = 0;; cntb++) { bool ok = true; for (int i = 0; i < 26; i++) ok &= ca[i] >= (cntb * cb[i]); if (!ok) break; int cntc = (int)(a.size()); for (int i = 0; i < 26; i++) if (cc[i]) cntc = min(cntc, (ca[i] - cntb * cb[i]) / cc[i]); if (cntb + cntc > ansb + ansc) { ansb = cntb; ansc = cntc; } } for (int i = 0; i < 26; i++) ca[i] -= (cb[i] * ansb + cc[i] * ansc); while (ansb--) cout << b; while (ansc--) cout << c; for (int i = 0; i < 26; i++) if (ca[i] > 0) while (ca[i]--) cout << (char)('a' + i); cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; string a, b, c; long long ca[26]; long long cb[26]; long long cc[26]; int main() { ios::sync_with_stdio(0); getline(cin, a); getline(cin, b); getline(cin, c); for (int i = 0; i < (int)a.size(); ++i) ca[a[i] - 'a']++; for (int i = 0; i < (int)b.size(); ++i) cb[b[i] - 'a']++; for (int i = 0; i < (int)c.size(); ++i) cc[c[i] - 'a']++; int ansb = 0; int ansc = 0; for (int i = 0; i < (int)a.size(); ++i) { bool can = 1; for (int j = 0; j < 26; ++j) { ca[j] -= i * cb[j]; if (ca[j] < 0) can = 0; } if (can) { long long cnt = 1e9; for (int j = 0; j < 26; ++j) if (cc[j]) cnt = min(cnt, ca[j] / cc[j]); if (i + cnt > ansc + ansb) { ansb = i; ansc = cnt; } } for (int j = 0; j < 26; ++j) ca[j] += i * cb[j]; } for (int j = 0; j < 26; ++j) { ca[j] -= ansb * cb[j]; ca[j] -= ansc * cc[j]; } for (int i = 0; i < ansc; ++i) printf("%s", c.c_str()); for (int i = 0; i < ansb; ++i) printf("%s", b.c_str()); for (int i = 0; i < 26; ++i) { for (int j = 0; j < ca[i]; ++j) printf("%c", (char)('a' + i)); } return 0; }
#include <bits/stdc++.h> using namespace std; long long dx[] = {1, 0, -1, 0}; long long dy[] = {0, 1, 0, -1}; long long gcd(long long x, long long y) { if (y == 0) return x; else return gcd(y, x % y); } long long expo(long long n, long long m, long long p) { long long r = 1; n = n % p; while (m > 0) { if (m % 2) r = (r * n) % p; n = (n * n) % p; m = m / 2; } return r % p; } bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; string a, b, c; cin >> a >> b >> c; long long ca[26] = {0}, cb[26] = {0}, cc[26] = {0}; long long mb = (1LL << 61); for (long long i = 0; i < a.size(); i++) { ca[a[i] - 'a']++; } for (long long i = 0; i < b.size(); i++) { cb[b[i] - 'a']++; } for (long long i = 0; i < c.size(); i++) { cc[c[i] - 'a']++; } for (long long i = 0; i < 26; i++) { if (cb[i] == 0) ; else mb = min(mb, ca[i] / cb[i]); } long long a1 = 0, a2 = 0; for (long long i = 0; i <= mb; i++) { long long ta1 = i, ta2 = (1LL << 61); long long A[26]; for (long long j = 0; j < 26; j++) { A[j] = ca[j]; A[j] -= i * cb[j]; } for (long long j = 0; j < 26; j++) { if (cc[j] == 0) ; else if (cc[j] != 0) ta2 = min(ta2, A[j] / cc[j]); } if (a1 + a2 < ta1 + ta2) { a1 = ta1, a2 = ta2; } } string ans; for (long long i = 0; i < a1; i++) { for (long long j = 0; j < b.size(); j++) { ans.push_back(b[j]); ca[b[j] - 'a']--; } } for (long long i = 0; i < a2; i++) { for (long long j = 0; j < c.size(); j++) { ans.push_back(c[j]); ca[c[j] - 'a']--; } } for (long long i = 0; i < 26; i++) { for (long long j = 0; j < ca[i]; j++) { ans.push_back('a' + i); } } cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 9; const unsigned long long INF = 1e9 + 7; const int base = 2e5 + 1; const long long MAX = 1e15 + 1; const double EPS = 1e-9; const double PI = acos(-1.); const int MAXN = 3 * 1e6 + 47; map<char, int> ca, cb, cc; pair<int, int> solve(string a, string b, string c) { for (auto t : a) ca[t]++; for (auto t : b) cb[t]++; for (auto t : c) cc[t]++; int x = MAXN, y = MAXN; for (int i = (0); i < (26); ++i) { char t = 'a' + i; if (cb[t]) { x = min(x, ca[t] / cb[t]); } } int mx = 0; pair<int, int> res; for (int z = (0); z < (x + 1); ++z) { y = MAXN; for (int i = (0); i < (26); ++i) { char t = 'a' + i; if (cc[t]) { y = min(y, max(0, (ca[t] - cb[t] * z) / cc[t])); } } if (z + y > mx) { mx = z + y; res = make_pair(z, y); } } return res; } int main() { string a, b, c; cin >> a >> b >> c; pair<int, int> p = solve(a, b, c); for (int i = (0); i < (p.first); ++i) cout << b; for (int i = (0); i < (p.second); ++i) cout << c; for (int i = (0); i < (26); ++i) { char t = char('a' + i); ca[t] -= p.first * cb[t]; ca[t] -= p.second * cc[t]; for (int j = (0); j < (ca[t]); ++j) cout << t; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long A = 100000000000000LL, N = 228228; string a, b, c; long long o[3], ok, _k[2] = {A, A}, d[4][26], i, j, n, m, r, k[2] = {A, A}; int main() { cin >> a >> b >> c, n = a.size(), m = b.size(), r = c.size(); for (i = 0; i < n; i++) d[0][a[i] - 'a']++; for (i = 0; i < 26; i++) d[3][i] = d[0][i]; for (i = 0; i < m; i++) d[1][b[i] - 'a']++; for (i = 0; i < r; i++) d[2][c[i] - 'a']++; for (i = 0; i < 26; i++) if (d[1][i]) k[0] = min(k[0], d[0][i] / d[1][i]); for (i = 0; i < 26; i++) if (d[2][i]) k[1] = min(k[1], d[0][i] / d[2][i]); for (i = 0; i <= k[0]; i++) { ok = 1; for (j = 0; j < 26; j++) if (d[0][j] < 0) ok = 0; if (!ok) break; r = A; for (j = 0; j < 26; j++) if (d[2][j]) r = min(r, d[0][j] / d[2][j]); if (i + r > o[0]) o[0] = i + r, o[1] = i, o[2] = r; for (j = 0; j < 26; j++) d[0][j] -= d[1][j]; } for (r = 0; r < 26; r++) d[0][r] = d[3][r]; for (r = 0; r < 26; r++) d[0][r] -= d[1][r] * o[1]; for (r = 0; r < 26; r++) d[0][r] -= d[2][r] * o[2]; if (o[1]) while (o[1]--) cout << b; if (o[2]) while (o[2]--) cout << c; for (i = 0; i < 26; i++) if (d[0][i]) while (d[0][i]--) cout << (char)(i + 'a'); }
#include <bits/stdc++.h> #pragma comment(linker, "/stack:20000000") using namespace std; long long m_a[26], m_b[26], m_c[26], temp[26]; int main() { char a[100010], b[100010], c[100010]; scanf("%s", a); scanf("%s%s", b, c); int len1 = strlen(a), len2 = strlen(b), len3 = strlen(c); for (int i = 0; i < len1; i++) { m_a[a[i] - 'a']++; } for (int i = 0; i < len2; i++) { m_b[b[i] - 'a']++; } for (int i = 0; i < len3; i++) { m_c[c[i] - 'a']++; } int tot = 0, tot_b = 0, tot_c = 0; for (int i = 0; i <= len1; i++) { long long ans = INT_MAX; bool np = true; for (int j = 0; j < 26; j++) { temp[j] = m_a[j] - m_b[j] * i; if (temp[j] < 0) { np = false; break; } } if (!np) continue; for (int j = 0; j < 26; j++) { if (m_c[j] == 0) continue; ans = min(ans, temp[j] / m_c[j]); } if (i + ans > tot) { tot = i + ans; tot_b = i; } } tot_c = tot - tot_b; for (int i = 0; i < tot_b; i++) printf("%s", b); for (int i = 0; i < tot_c; i++) printf("%s", c); for (int i = 0; i < 26; i++) { m_a[i] -= (m_b[i] * tot_b + m_c[i] * tot_c); for (int j = 0; j < m_a[i]; j++) { printf("%c", (char)i + 'a'); } } printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; long long cnta[333]; long long cntb[333]; long long cntc[333]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string a, b, c; cin >> a >> b >> c; for (auto x : a) { cnta[x]++; } for (auto x : b) { cntb[x]++; } for (auto x : c) { cntc[x]++; } int n = a.size(); pair<int, int> ans = {0, 0}; for (int i = 0; i <= n; i++) { int j = n; for (int z = 'a'; z <= 'z'; z++) { long long v = (cnta[z] - i * cntb[z]); if (v < 0) { j = -1; break; } if (cntc[z] == 0) continue; j = min(v / cntc[z], (long long)j); } if (j < 0) break; ans = max(ans, {i + j, j}); } ans.first -= ans.second; for (int i = 0; i < ans.first; i++) { cout << b; for (auto x : b) { cnta[x]--; } } for (int i = 0; i < ans.second; i++) { cout << c; for (auto x : c) { cnta[x]--; } } for (int i = 'a'; i <= 'z'; i++) { for (int j = 0; j < cnta[i]; j++) { cout << (char)i; } } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> toAlphaCount(const string& s) { vector<int> res(26, 0); for (char c : s) { ++res[c - 'a']; } return res; } int main() { string a, b, c; cin >> a >> b >> c; vector<int> cntA = toAlphaCount(a); vector<int> cntB = toAlphaCount(b); vector<int> cntC = toAlphaCount(c); int maxi = 0, maxiUseB = 0, maxiUseC = 0; for (int useB = 0;; useB++) { vector<int> rem = cntA; bool ok = true; for (int i = 0; i < cntB.size(); i++) { rem[i] -= cntB[i] * useB; if (rem[i] < 0) { ok = false; break; } } if (!ok) break; int useC = 1 << 25; for (int i = 0; i < cntC.size(); i++) { if (cntC[i] > 0) { useC = min(useC, rem[i] / cntC[i]); } } int cnt = useB + useC; if (cnt > maxi) { maxi = cnt; maxiUseB = useB; maxiUseC = useC; } } string res; for (int i = 0; i < maxiUseB; i++) res += b; for (int i = 0; i < maxiUseC; i++) res += c; for (int i = 0; i < cntA.size(); i++) { int rem = cntA[i] - cntB[i] * maxiUseB - cntC[i] * maxiUseC; res += string(rem, 'a' + i); } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; char a[100010], b[100010], c[100010]; int fa[50], fb[50], fc[50], faux[50]; bool verif(int k) { int minim = 100010; for (int i = 0; i <= 30; i++) { if (fb[i]) minim = min(minim, fa[i] / fb[i]); } return (minim >= k); } int main() { cin >> a; cin >> b; cin >> c; int na = strlen(a); int nb = strlen(b); int nc = strlen(c); for (int i = 0; i < na; i++) fa[a[i] - 'a']++; for (int i = 0; i < nb; i++) fb[b[i] - 'a']++; for (int i = 0; i < nc; i++) fc[c[i] - 'a']++; int k = 0; int maxim = 0; int maxa = 0; int maxb = 0; while (true) { if (!verif(k)) break; for (int i = 0; i <= 30; i++) fa[i] -= k * fb[i]; int minim = 100010; for (int i = 0; i <= 30; i++) { if (fc[i]) minim = min(minim, fa[i] / fc[i]); } if (minim + k > maxim) { maxim = minim + k; maxa = k; maxb = minim; } for (int i = 0; i <= 30; i++) fa[i] += k * fb[i]; k++; } for (int i = 1; i <= maxa; i++) for (int j = 0; j < nb; j++) cout << b[j]; for (int i = 1; i <= maxb; i++) for (int j = 0; j < nc; j++) cout << c[j]; for (int i = 0; i <= 30; i++) fa[i] -= (maxa * fb[i] + maxb * fc[i]); for (int i = 0; i <= 30; i++) { if (fa[i]) { char c = i + 'a'; while (fa[i]--) cout << c; } } return 0; }
#include <bits/stdc++.h> using namespace std; int a[30], b[30], c[30]; string s, s1, s2; int i, j; int main() { cin >> s >> s1 >> s2; for (auto ch : s) a[ch - 'a']++; for (auto ch : s1) b[ch - 'a']++; for (auto ch : s2) c[ch - 'a']++; int m = 0x3f3f3f3f, n = 0x3f3f3f3f, bb = 0, cc = 0; for (i = 0; i <= 26; i++) if (b[i] != 0) m = min(m, a[i] / b[i]); for (i = 0; i <= m; i++) { for (j = 0; j <= 26; j++) if (c[j] != 0) n = min(n, (a[j] - i * b[j]) / c[j]); if (i + n > bb + cc) bb = i, cc = n; } for (i = 1; i <= bb; i++) cout << s1; for (i = 1; i <= cc; i++) cout << s2; for (i = 0; i <= 26; i++) if (a[i] - bb * b[i] - cc * c[i] > 0) cout << char('a' + i), a[i]--, i--; }
#include <bits/stdc++.h> using namespace std; inline char skipwhitespace() { char ch = getchar(); while (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t') ch = getchar(); return ch; } void gi(int &x) { register int c = getchar(); x = 0; int sn = 1; for (; (c < 48 || c > 57); c = getchar()) if (c == '-') sn = -1; for (; c > 47 && c < 58; c = getchar()) { x = (x << 1) + (x << 3) + c - 48; } x *= sn; } void gii(long long int &x) { register long long int c = getchar(); x = 0; long long int sn = 1; for (; (c < 48 || c > 57); c = getchar()) if (c == '-') sn = -1; for (; c > 47 && c < 58; c = getchar()) { x = (x << 1) + (x << 3) + c - 48; } x *= sn; } void wi(int n) { int sign = n >= 0 ? 1 : -1; n = n > 0 ? n : -n; char buf[10]; int i = 9; if (!n) buf[i--] = 48; while (n) { buf[i--] = n % 10 + 48; n /= 10; } if (sign < 0) printf("%c", '-'); while (++i < 10) printf("%c", buf[i]); } void wii(long long int n) { long long int sign = n >= 0 ? 1 : -1; n = n > 0 ? n : -n; char buf[25]; long long int i = 24; if (!n) buf[i--] = 48; while (n) { buf[i--] = n % 10 + 48; n /= 10; } if (sign < 0) printf("%c", '-'); while (++i < 25) printf("%c", buf[i]); } int gs(char *s) { char ch = skipwhitespace(); int n = 0; while ((ch != '\n' && ch != ' ' && ch != '\t')) { s[n++] = ch; ch = getchar(); } s[n] = 0; return n; } void ws(char *s) { char *p = s; while (*p) { printf("%c", *p); p++; } } char str1[1000000], str2[1000000], str3[1000000]; int n1, n2, n3, a[30] = {0}, b[30] = {0}, c[30] = {0}, l = 1000000, x = 0, y = 0, flag = 1, tx, ty; int main() { scanf("%s%s%s", str1, str2, str3); n1 = strlen(str1); n2 = strlen(str2); n3 = strlen(str3); for (int i = 0; i < n1; i++) a[str1[i] - 'a']++; for (int i = 0; i < n2; i++) b[str2[i] - 'a']++; for (int i = 0; i < n3; i++) c[str3[i] - 'a']++; for (int i = 0; i < 30; i++) if (b[i]) l = min(l, a[i] / b[i]), flag = 0; if (flag) l = 0; for (int i = 0; i < l + 1; i++) { tx = i; ty = 1000000; for (int j = 0; j < 30; j++) if (c[j]) ty = min(ty, (a[j] - b[j] * tx) / c[j]); if (tx >= 0 && ty >= 0 && x + y < tx + ty) x = tx, y = ty; } for (int i = 0; i < n2; i++) a[str2[i] - 'a'] -= x; for (int j = 0; j < n3; j++) a[str3[j] - 'a'] -= y; while (x > 0) ws(str2), x--; while (y > 0) ws(str3), y--; for (int i = 0; i < 30; i++) while (a[i] > 0) cout << (char)('a' + i), a[i]--; printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { string a, b, c; cin >> a >> b >> c; vector<long long> cnt1(26), cnt2(26), cnt3(26); for (auto& it : a) cnt1[it - 'a']++; for (auto& it : b) cnt2[it - 'a']++; for (auto& it : c) cnt3[it - 'a']++; long long max_occur_b = 2e9; for (long long i = 0; i < 26; i++) { if (cnt2[i] > 0) { long long take = cnt1[i] / cnt2[i]; max_occur_b = min(max_occur_b, take); } } long long count_b = 0, count_c = 0; for (long long occur_b = 0; occur_b <= max_occur_b; occur_b++) { long long occur_c = max_occur_b - occur_b; long long max_poss = 2e9; for (long long i = 0; i < 26; i++) { if (cnt3[i] > 0) { long long remain = cnt1[i] - occur_b * cnt2[i]; assert(remain >= 0); max_poss = min(max_poss, remain / cnt3[i]); } } if (occur_b + max_poss > count_b + count_c) { count_b = occur_b, count_c = max_poss; } } for (long long i = 0; i < count_b; i++) { cout << b; } for (long long i = 0; i < count_c; i++) { cout << c; } for (long long i = 0; i < 26; i++) { long long remain = cnt1[i] - (count_b * cnt2[i] + count_c * cnt3[i]); assert(remain >= 0); while (remain--) cout << (char)(i + 'a'); } cout << '\n'; } signed main() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string a, b, c; cin >> a >> b >> c; long long x = 0, y = 0; long long tot[500] = {0}; for (long long i = 0; i < a.length(); i++) tot[a[i]]++; long long cl[500] = {0}; long long bl[500] = {0}; for (long long i = 0; i < b.length(); i++) bl[b[i]]++; for (long long i = 0; i < c.length(); i++) cl[c[i]]++; x = a.length(); int xma = 0, yma = 0; long long ma = 0; while (x >= 0) { y = a.length() + 1; bool check = true; for (long long i = 0; i < 500; i++) { if (x * bl[i] > tot[i]) { check = false; break; } if (cl[i] > 0) y = min(y, (tot[i] - x * bl[i]) / cl[i]); } if (y == a.length() + 1) y = 0; if (check && x + y > ma) { ma = x + y; xma = x; yma = y; } --x; } for (int i = 0; i < xma; i++) cout << b; for (int i = 0; i < yma; i++) cout << c; for (int i = 0; i < 500; i++) { tot[i] -= (xma * bl[i] + yma * cl[i]); while (tot[i] > 0) { cout << (char)i; --tot[i]; } } cout << endl; }
#include <bits/stdc++.h> using namespace std; int ca[26], cb[26], cc[26]; int check(int *x, int *y) { int c = 999999; for (int i = 0; i < 26; i++) { if (y[i] > 0 && x[i] == 0) return 0; if (y[i] > 0) { int t = x[i] / y[i]; c = min(c, t); } } return c; } void cut(int *x, int *y) { for (int i = 0; i < 26; i++) x[i] -= y[i]; } int main() { string a, b, c; cin >> a >> b >> c; for (int i = 0; a[i]; i++) ca[a[i] - 'a']++; for (int i = 0; b[i]; i++) cb[b[i] - 'a']++; for (int i = 0; c[i]; i++) cc[c[i] - 'a']++; string sol = ""; while (1) { int t1 = check(ca, cb); int t2 = check(ca, cc); if (!t1 && !t2) break; if (t1 > t2) { sol += b; cut(ca, cb); } else { sol += c; cut(ca, cc); } } for (int i = 0; i < 26; i++) while (ca[i]) { sol += ('a' + i); ca[i]--; } cout << sol << endl; return 0; }
#include <bits/stdc++.h> using namespace std; string a, b, c; int occA[26], occB[26], occC[26]; int main() { cin >> a >> b >> c; memset(occA, 0, sizeof occA); memset(occB, 0, sizeof occB); memset(occC, 0, sizeof occC); for (int i = 0; i < a.length(); i++) occA[a[i] - 'a']++; for (int i = 0; i < b.length(); i++) occB[b[i] - 'a']++; for (int i = 0; i < c.length(); i++) occC[c[i] - 'a']++; int Bs = (int)1e9; for (int i = 0; i < 26; i++) if (occB[i]) Bs = min(Bs, occA[i] / occB[i]); int ansBs = 0, ansCs = 0; for (int i = 0; i < Bs + 1; i++) { for (int j = 0; j < 26; j++) occA[j] -= occB[j] * i; int Cs = (int)1e9; for (int j = 0; j < 26; j++) if (occC[j]) Cs = min(Cs, occA[j] / occC[j]); if (i + Cs > ansBs + ansCs) { ansBs = i; ansCs = Cs; } for (int j = 0; j < 26; j++) occA[j] += occB[j] * i; } string res = ""; for (int i = 0; i < ansBs; i++) res += b; for (int i = 0; i < ansCs; i++) res += c; for (int i = 0; i < 26; i++) { occA[i] -= occB[i] * ansBs; occA[i] -= occC[i] * ansCs; for (int j = 0; j < occA[i]; j++) res += (i + 'a'); } cout << res << '\n'; }
#include <bits/stdc++.h> char a[100024]; char b[100024]; char c[100024]; char buf[200024]; int visa[30]; int visb[30]; int visc[30]; int tee[30]; int len, n; int minn, mnn; int ans1, ans2; int main() { scanf("%s%s%s", a, b, c); for (int i = (0), __ = (strlen(a)); i < __; ++i) ++visa[a[i] - 'a']; for (int i = (0), __ = (strlen(b)); i < __; ++i) ++visb[b[i] - 'a']; for (int i = (0), __ = (strlen(c)); i < __; ++i) ++visc[c[i] - 'a']; minn = 1000024; for (int i = (0), __ = (30); i < __; ++i) if (visb[i]) { if (minn > visa[i] / visb[i]) { minn = visa[i] / visb[i]; } } for (int it = (0), __ = (minn); it <= __; ++it) { mnn = 1000024; for (int i = (0), __ = (30); i < __; ++i) if (visc[i]) { tee[i] = visa[i] - it * visb[i]; if (mnn > tee[i] / visc[i]) { mnn = tee[i] / visc[i]; } } if (mnn != 1000024 && mnn + it > ans1 + ans2) { ans1 = it; ans2 = mnn; } } for (int i = (0), __ = (ans1); i < __; ++i) printf("%s", b); for (int i = (0), __ = (ans2); i < __; ++i) printf("%s", c); for (int i = (0), __ = (30); i < __; ++i) { len = visa[i] - visb[i] * ans1 - visc[i] * ans2; if (len > 0) { while (len--) putchar(i + 'a'); } } puts(""); return 0; }
#include <bits/stdc++.h> int main() { char str_a[100001], str_b[100001], str_c[100001]; int letter_a[26] = {0}, letter_b[26] = {0}, letter_c[26] = {0}; ~scanf("%s\n%s\n%s", str_a, str_b, str_c); int len_a = strlen(str_a), len_b = strlen(str_b), len_c = strlen(str_c); for (int i = 0; i < len_a; i++) letter_a[str_a[i] - 'a']++; for (int i = 0; i < len_b; i++) letter_b[str_b[i] - 'a']++; for (int i = 0; i < len_c; i++) letter_c[str_c[i] - 'a']++; int max_num = 0, b_num = 0, c_num = 0; for (int i = 0; i <= 500; i++) { for (int j = 0; j <= 500; j++) { if (letter_b[0] * i + letter_c[0] * j <= letter_a[0] && letter_b[1] * i + letter_c[1] * j <= letter_a[1] && letter_b[2] * i + letter_c[2] * j <= letter_a[2] && letter_b[3] * i + letter_c[3] * j <= letter_a[3] && letter_b[4] * i + letter_c[4] * j <= letter_a[4] && letter_b[5] * i + letter_c[5] * j <= letter_a[5] && letter_b[6] * i + letter_c[6] * j <= letter_a[6] && letter_b[7] * i + letter_c[7] * j <= letter_a[7] && letter_b[8] * i + letter_c[8] * j <= letter_a[8] && letter_b[9] * i + letter_c[9] * j <= letter_a[9] && letter_b[10] * i + letter_c[10] * j <= letter_a[10] && letter_b[11] * i + letter_c[11] * j <= letter_a[11] && letter_b[12] * i + letter_c[12] * j <= letter_a[12] && letter_b[13] * i + letter_c[13] * j <= letter_a[13] && letter_b[14] * i + letter_c[14] * j <= letter_a[14] && letter_b[15] * i + letter_c[15] * j <= letter_a[15] && letter_b[16] * i + letter_c[16] * j <= letter_a[16] && letter_b[17] * i + letter_c[17] * j <= letter_a[17] && letter_b[18] * i + letter_c[18] * j <= letter_a[18] && letter_b[19] * i + letter_c[19] * j <= letter_a[19] && letter_b[20] * i + letter_c[20] * j <= letter_a[20] && letter_b[21] * i + letter_c[21] * j <= letter_a[21] && letter_b[22] * i + letter_c[22] * j <= letter_a[22] && letter_b[23] * i + letter_c[23] * j <= letter_a[23] && letter_b[24] * i + letter_c[24] * j <= letter_a[24] && letter_b[25] * i + letter_c[25] * j <= letter_a[25]) { if (i + j > max_num) { max_num = i + j; b_num = i; c_num = j; } } else { break; } } } for (int i = 0; i < b_num; i++) printf("%s", str_b); for (int i = 0; i < c_num; i++) printf("%s", str_c); for (int i = 0; i < 26; i++) { letter_a[i] -= (letter_b[i] * b_num + letter_c[i] * c_num); } for (int i = 0; i < 26; i++) while (letter_a[i]--) printf("%c", 'a' + i); printf("\n"); }
#include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; template <class T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } inline long long expo(long long base, int nent) { if (nent == 1) return base; else if (nent & 1) { long long temp = expo(base, nent / 2); temp = (temp * temp) % MOD * base; if (temp >= MOD) temp %= MOD; return temp; } else { long long temp = expo(base, nent / 2); temp *= temp; if (temp >= MOD) temp %= MOD; return temp; } } int hs[26], ha[26], hb[26], c[26], m1 = MOD, m2 = MOD, ans, temp, a1, a2, prev; bool f; int main() { string s, a, b, res; cin >> s >> a >> b; for (int i = 0; i < (int)s.length(); i++) hs[s[i] - 97]++; for (int i = 0; i < (int)a.length(); i++) ha[a[i] - 97]++; for (int i = 0; i < (int)b.length(); i++) hb[b[i] - 97]++; for (int i = 0; i < (int)26; i++) if (ha[i]) m1 = min(hs[i] / ha[i], m1); for (int i = 0; i < (int)26; i++) if (hb[i]) m2 = min(hs[i] / hb[i], m2); for (int i = 0; i < (int)26; i++) if (ha[i] != hb[i]) { f = 1; break; } if (m1 && m2 && f) { f = 0; if (m1 > m2) ans = a1 = m1; else ans = a2 = m2; temp = m2; for (int j = 0; j < (int)26; j++) c[j] = hs[j]; for (int i = 1; i < (int)m1 + 1; i++) { if (f) break; for (int j = 0; j < (int)26; j++) c[j] -= ha[j]; for (int j = temp; j >= 0; j--) { if (!j) { f = 1; break; } f = 0; for (int k = 0; k < (int)26; k++) if (c[k] - j * hb[k] < 0) { f = 1; break; } if (!f) { temp = j; if (i + j > ans) ans = i + j, a1 = i, a2 = j; break; } } } } else if (!f) ans = a1 = m1; else if (m1) ans = a1 = m1; else ans = a2 = m2; res = ""; for (int i = 0; i < (int)a1; i++) res += a; for (int i = 0; i < (int)a2; i++) res += b; for (int i = 0; i < (int)26; i++) hs[i] -= (a1 * ha[i] + a2 * hb[i]); for (int i = 0; i < (int)26; i++) for (int j = 0; j < (int)hs[i]; j++) res += (char)(97 + i); cout << res; return 0; }
#include <bits/stdc++.h> using namespace std; double const EPS = 2.22045e-016; void print_v(vector<int> arr) { int n = ((int)((arr).size())); for (int i = 0; i < (int)(n); ++i) if (i == n - 1) printf("%d\n", arr[i]); else printf("%d ", arr[i]); } int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); } int main() { string a, b, c; cin >> a >> b >> c; vector<int> a_h(30, 0), b_h(30, 0), c_h(30, 0); for (int i = 0; i < (int)(((int)((a).size()))); ++i) a_h[a[i] - 'a']++; for (int i = 0; i < (int)(((int)((b).size()))); ++i) b_h[b[i] - 'a']++; for (int i = 0; i < (int)(((int)((c).size()))); ++i) c_h[c[i] - 'a']++; int const MAX = 100005; int ans1, ans2 = 0; for (int t = 0; t < (int)(MAX); ++t) { vector<int> a_temp(a_h); bool g = true; for (int i = 0; i < (int)(30); ++i) { if (b_h[i] * t > a_temp[i]) { g = false; break; } a_temp[i] -= b_h[i] * t; } if (!g) break; g = true; int mn = MAX; for (int i = 0; i < (int)(30); ++i) { if (c_h[i] > a_temp[i]) { g = false; break; } if (c_h[i] != 0) mn = min(mn, a_temp[i] / c_h[i]); } if (mn == MAX || !g) mn = 0; if (ans2 + ans1 < t + mn) { ans1 = t; ans2 = mn; } } for (int i = 0; i < (int)(30); ++i) { a_h[i] -= (b_h[i] * ans1 + c_h[i] * ans2); } for (int i = 0; i < (int)(ans1); ++i) cout << b; for (int i = 0; i < (int)(ans2); ++i) cout << c; for (int i = 0; i < (int)(30); ++i) { for (int k = 0; k < (int)(a_h[i]); ++k) printf("%c", i + 'a'); } printf("\n"); return 0; }
#include <bits/stdc++.h> char a[100000 + 10]; char b[100000 + 10]; char c[100000 + 10]; int _hash[200]; int _hash2[200]; int _hash3[200]; int numb, numc; int main() { while (scanf("%s%s%s", a, b, c) != EOF) { memset(_hash, 0, sizeof(_hash)); memset(_hash2, 0, sizeof(_hash2)); memset(_hash3, 0, sizeof(_hash3)); int len = strlen(a); int len2 = strlen(b); int len3 = strlen(c); for (int i = 0; i < len; i++) { _hash[a[i]]++; } for (int i = 0; i < len2; i++) { _hash2[b[i]]++; } for (int i = 0; i < len3; i++) { _hash3[c[i]]++; } numb = 1000000; int maxnumb; int maxsum = -1000; int maxnum; for (int i = 0; i < 200; i++) { if (_hash2[i] != 0) if (_hash[i] / _hash2[i] < numb) numb = _hash[i] / _hash2[i]; } for (int i = 0; i <= numb; i++) { for (int j = 0; j < 200; j++) { if (_hash2[j] != 0) _hash[j] -= i * _hash2[j]; } maxnum = 1000000; for (int j = 0; j < 200; j++) { if (_hash3[j] != 0) if (_hash[j] / _hash3[j] < maxnum) maxnum = _hash[j] / _hash3[j]; } for (int j = 0; j < 200; j++) { if (_hash2[j] != 0) _hash[j] += i * _hash2[j]; } if (i + maxnum > maxsum) { maxnumb = i; numc = maxnum; maxsum = i + maxnum; } } for (int i = 0; i < maxnumb; i++) printf("%s", b); for (int i = 0; i < numc; i++) printf("%s", c); for (int i = 0; i < 200; i++) { if (_hash2[i] != 0) _hash[i] -= maxnumb * _hash2[i]; if (_hash3[i] != 0) _hash[i] -= numc * _hash3[i]; } for (int i = 0; i < 200; i++) { if (_hash[i] != 0) for (int j = 0; j < _hash[i]; j++) printf("%c", (char)i); } printf("\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); string a, b, c; cin >> a >> b >> c; map<char, int> x, y, z; for (auto i : a) { x[i]++; } for (auto i : b) { y[i]++; } for (auto i : c) { z[i]++; } int cnt = 0, ans = 0; int f = 0; bool st; for (int i = 0, st = true; st; ++i) { int pos = 1e9; for (auto [symb, cnt] : z) { pos = min(pos, x[symb] / cnt); } if (ans < i + pos) { ans = i + pos; cnt = i; f = pos; } for (auto [symb, cnt] : y) { if (x[symb] < cnt) { st = false; break; } x[symb] -= cnt; } } x.clear(); for (auto i : a) { x[i]++; } for (int i = 0; i < cnt; ++i) { cout << b; for (auto [symb, cnt] : y) { x[symb] -= cnt; } } for (int i = 0; i < f; ++i) { cout << c; for (auto [symb, cnt] : z) { x[symb] -= cnt; } } for (auto [symb, cnt] : x) { for (int j = 0; j < cnt; ++j) { cout << symb; } } }
#include <bits/stdc++.h> using namespace std; char a[100010], b[100010], c[100010]; int cnt_a[26], cnt_b[26], cnt_c[26]; int remain[26], len_a, len_b, len_c; int c_num, max_b, max_c; void subtract(int b_num) { if (b_num * len_b > len_a) return; for (int i = 0; i < 26; i++) { remain[i] = cnt_a[i] - b_num * cnt_b[i]; if (remain[i] < 0) return; } int c_num = 200000; for (int i = 0; i < 26; i++) if (cnt_c[i] != 0) if (remain[i] / cnt_c[i] < c_num) c_num = remain[i] / cnt_c[i]; if (c_num != 200000 && b_num + c_num > max_b + max_c) max_b = b_num, max_c = c_num; subtract(b_num + 1); } int main() { while (fgets(a, sizeof(a), stdin) != NULL) { fgets(b, sizeof(b), stdin); fgets(c, sizeof(c), stdin); memset(cnt_a, 0, sizeof(cnt_a)); memset(cnt_b, 0, sizeof(cnt_b)); memset(cnt_c, 0, sizeof(cnt_c)); len_a = strlen(a), len_b = strlen(b), len_c = strlen(c); a[len_a - 1] = 0, b[len_b - 1] = 0, c[len_c - 1] = 0; len_a--, len_b--, len_c--; for (int i = 0; i < len_a; i++) cnt_a[a[i] - 'a']++; for (int i = 0; i < len_b; i++) cnt_b[b[i] - 'a']++; for (int i = 0; i < len_c; i++) cnt_c[c[i] - 'a']++; max_b = 0, max_c = 0; subtract(0); for (int i = 1; i <= max_b; i++) printf("%s", b); for (int i = 1; i <= max_c; i++) printf("%s", c); for (int i = 0; i < 26; i++) { for (int j = 1; j <= cnt_a[i] - max_b * cnt_b[i] - max_c * cnt_c[i]; j++) printf("%c", i + 'a'); } printf("\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; char a[100005], b[100005], c[100005]; vector<char> arr1, arr2, val; map<char, int> mp1, mp2, mp3; int main() { cin >> a; cin >> b; cin >> c; int len2 = strlen(b); for (int i = 0; i < len2; i++) { if (mp1.count(b[i]) == 0) { arr1.push_back(b[i]); mp1[b[i]] = 1; } else { mp1[b[i]]++; } } int len3 = strlen(c); for (int i = 0; i < len3; i++) { if (mp2.count(c[i]) == 0) { arr2.push_back(c[i]); mp2[c[i]] = 1; } else { mp2[c[i]]++; } } int len1 = strlen(a); for (int i = 0; i < len1; i++) { if (mp3.count(a[i]) == 0) { val.push_back(a[i]); mp3[a[i]] = 1; } else { mp3[a[i]]++; } } int temp1 = 123123; for (int i = 0; i < arr1.size(); i++) { int calc = mp3[arr1[i]] / mp1[arr1[i]]; temp1 = min(temp1, calc); } int temp2 = 123123; for (int i = 0; i < arr2.size(); i++) { int calc = mp3[arr2[i]] / mp2[arr2[i]]; temp2 = min(calc, temp2); } int sum = -123123; int bc = 0, mc = 0; for (int i = 0; i <= temp1; i++) { for (int j = 0; j < arr1.size(); j++) { mp3[arr1[j]] -= (i * mp1[arr1[j]]); } int temp3 = 123123; for (int j = 0; j < arr2.size(); j++) { int calc = mp3[arr2[j]] / mp2[arr2[j]]; temp3 = min(calc, temp3); } for (int j = 0; j < arr1.size(); j++) { mp3[arr1[j]] += (i * mp1[arr1[j]]); } if (i + temp3 > sum) { bc = i; mc = temp3; sum = max(sum, i + temp3); } } for (int i = 0; i < bc; i++) { printf("%s", b); } for (int i = 0; i < mc; i++) { printf("%s", c); } for (int i = 0; i < arr1.size(); i++) { mp3[arr1[i]] -= (bc * mp1[arr1[i]]); } for (int i = 0; i < arr2.size(); i++) { mp3[arr2[i]] -= (mc * mp2[arr2[i]]); } for (int i = 0; i < val.size(); i++) { for (int j = 0; j < mp3[val[i]]; j++) { printf("%c", val[i]); } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string a, b, c, aux; cin >> a >> b >> c; int aa[26] = {0}, bb[26] = {0}, cc[26] = {0}, cntb = 0, cntc = 0, cntbb = 2000000; for (int i = 0; i < a.length(); i++) { aa[a[i] - 'a']++; } for (int i = 0; i < b.length(); i++) { bb[b[i] - 'a']++; } for (int i = 0; i < c.length(); i++) { cc[c[i] - 'a']++; } for (int i = 0; i < 26; i++) { if (bb[i] != 0 && aa[i] / bb[i] < cntbb) cntbb = aa[i] / bb[i]; } for (int i = 0; i <= cntbb; i++) { int res = 2000000; for (int j = 0; j < 26; j++) { if (cc[j] != 0 && (aa[j] - i * bb[j]) / cc[j] < res) res = (aa[j] - i * bb[j]) / cc[j]; } if (i + res > cntc + cntb) { cntb = i; cntc = res; } } for (int i = 0; i < cntb; i++) cout << b; for (int i = 0; i < cntc; i++) cout << c; for (int i = 0; i < 26; i++) { for (int j = 0; j < aa[i] - (cntb * bb[i] + cntc * cc[i]); j++) cout << char(i + 'a'); } cout << endl; }
#include <bits/stdc++.h> using namespace std; template <class T> T sqr(const T &a) { return a * a; } const int MAXN = 1e5 + 100; char a[MAXN], b[MAXN], c[MAXN]; int n, m, k, cnt[30], tmp[30], x[30], ans, ans1, ans2; int main() { gets(a); gets(b); gets(c); n = strlen(a); m = strlen(b); k = strlen(c); for (int i = 0; i < (int)n; i++) cnt[a[i] - 'a']++; for (int i = 0; i < (int)n; i++) { if (i * m > n) break; for (int j = 0; j < (int)26; j++) tmp[j] = cnt[j]; int good = 1; for (int j = 0; j < (int)m; j++) { tmp[b[j] - 'a'] -= i; if (tmp[b[j] - 'a'] < 0) good = 0; } if (!good) break; good = 1e9; for (int j = 0; j < (int)26; j++) x[j] = 0; for (int j = 0; j < (int)k; j++) x[c[j] - 'a']++; for (int j = 0; j < (int)26; j++) if (x[j]) good = min(good, tmp[j] / x[j]); if (ans < i + good) { ans = i + good; ans1 = i; ans2 = good; } } for (int i = 0; i < (int)ans1; i++) printf("%s", b); for (int i = 0; i < (int)ans2; i++) printf("%s", c); for (int i = 0; i < (int)m; i++) cnt[b[i] - 'a'] -= ans1; for (int i = 0; i < (int)k; i++) cnt[c[i] - 'a'] -= ans2; for (int i = 0; i < (int)26; i++) for (int j = 0; j < (int)cnt[i]; j++) printf("%c", i + 'a'); puts(""); }
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:200000000") int cntA[26], cntB[26], cntC[26]; int scnt[26]; int main() { string a, b, c; cin >> a >> b >> c; for (int i = 0, maxi = (int)(a).size(); i < maxi; i++) { cntA[(a[i] - 'a')]++; scnt[(a[i] - 'a')]++; } for (int i = 0, maxi = (int)(b).size(); i < maxi; i++) { cntB[(b[i] - 'a')]++; } for (int i = 0, maxi = (int)(c).size(); i < maxi; i++) { cntC[(c[i] - 'a')]++; } int bestres = 0; int bestcnt = 0; for (int i = 0, maxi = (int)(a).size() / (int)(b).size(); i <= maxi; i++) { for (int j = 0; j < 26; j++) cntA[j] = scnt[j]; bool stop = false; for (int j = 0; j < 26; j++) { if (cntA[j] < cntB[j] * i) { stop = true; break; } } if (stop) break; int ost = 1e9; for (int j = 0; j < 26; j++) { int cur = cntA[j] - cntB[j] * i; if (cntC[j] != 0) ost = min(ost, cur / cntC[j]); } if (ost + i > bestcnt) bestcnt = ost + i, bestres = i; } string res = ""; for (int i = 0; i < bestres; i++) { res += b; } int ost = 1e9; for (int j = 0; j < 26; j++) { int cur = cntA[j] - cntB[j] * bestres; if (cntC[j] != 0) ost = min(ost, cur / cntC[j]); } for (int j = 0; j < 26; j++) { cntA[j] -= cntB[j] * bestres; } for (int i = 0; i < ost; i++) { res += c; } for (int j = 0; j < 26; j++) { cntA[j] -= cntC[j] * ost; } for (int j = 0; j < 26; j++) { for (int i = 0; i < cntA[j]; i++) { res += (j + 'a'); } } cout << res << endl; return 0; }
#include <bits/stdc++.h> const int INF = 0x3f3f3f; const int MAX = 100005; using namespace std; int a[30], b[30], c[30]; char c1[MAX], c2[MAX], c3[MAX]; char ch; int main() { int I = 0; while (~scanf("%c", &ch)) { c1[I++] = ch; if (ch == '\n') break; a[ch - 'a']++; } I = 0; while (~scanf("%c", &ch)) { c2[I++] = ch; if (ch == '\n') break; b[ch - 'a']++; } I = 0; while (~scanf("%c", &ch)) { c3[I++] = ch; if (ch == '\n') break; c[ch - 'a']++; } int m = INF; for (int i = 0; i < 26; i++) { if (b[i] == 0) continue; m = min(m, a[i] / b[i]); } int ans = 0, ans1 = 0, ans2 = 0; for (int i = 0; i <= m; i++) { int n = INF; for (int j = 0; j < 26; j++) { if (c[j] == 0) continue; n = min(n, (a[j] - i * b[j]) / c[j]); } if (ans < n + i) { ans1 = i; ans2 = n; ans = n + i; } } for (int i = 0; i < 26; i++) { a[i] -= (b[i] * ans1 + c[i] * ans2); } while (ans1--) { for (int i = 0; c2[i] != '\n'; i++) { printf("%c", c2[i]); } } while (ans2--) { for (int i = 0; c3[i] != '\n'; i++) { printf("%c", c3[i]); } } for (int i = 0; i < 26; i++) { for (int j = 0; j < a[i]; j++) { printf("%c", 'a' + i); } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); string a, b, c; cin >> a >> b >> c; int a1[30] = {0}, a2[30] = {0}, a3[30] = {0}, i, ansa = 0, ansb = 0; for (i = 0; i < a.length(); i++) a1[a[i] - 'a']++; for (i = 0; i < b.length(); i++) a2[b[i] - 'a']++; for (i = 0; i < c.length(); i++) a3[c[i] - 'a']++; int sum = 999999; for (i = 0; i < 26; i++) { if (a2[i]) sum = min(sum, a1[i] / a2[i]); } for (i = 0; i <= sum; i++) { int val = 999999; for (int j = 0; j < 26; j++) { if (a3[j]) val = min(val, (a1[j] - a2[j] * i) / a3[j]); } if (i + val > ansa + ansb) { ansa = i; ansb = val; } } for (i = 0; i < ansa; i++) cout << b; for (i = 0; i < ansb; i++) cout << c; for (i = 0; i < 26; i++) for (int j = 0; j < a1[i] - a2[i] * ansa - a3[i] * ansb; j++) cout << (char)(i + 'a'); cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename A, typename B> int print_value(A title, B val) { cout << title << " = " << val << "\n"; return 1; } int break_point() { char c; while ((c = getchar()) != '\n') ; return 0; } template <typename A> int logg(A v) { cout << v << "\n"; return 0; } void run(); int main() { srand(time(NULL)); do { run(); if (0) { 0 ? printf("-------------------------------\n") : 0; 0 ? printf("-------------------------------\n") : 0; } } while (0); return 0; } int ad[26], bd[26], cd[26]; char a[100005], b[100005], c[100005]; void run() { scanf("%s", a); int al = strlen(a); scanf("%s", b); int bl = strlen(b); scanf("%s", c); int cl = strlen(c); for (int i = 0; i < al; i++) ad[a[i] - 'a']++; for (int i = 0; i < bl; i++) bd[b[i] - 'a']++; for (int i = 0; i < cl; i++) cd[c[i] - 'a']++; int bn = INT_MAX; int rb = 0, rc = 0; for (int i = 0; i < 26; i++) { if (bd[i]) bn = min(bn, ad[i] / bd[i]); } for (int i = 0; i <= bn; i++) { int cn = INT_MAX; for (int j = 0; j < 26; j++) { int d = ad[j] - i * bd[j]; if (cd[j]) cn = min(cn, d / cd[j]); } if (i + cn > rb + rc) { rb = i; rc = cn; } } for (int i = 0; i < 26; i++) { ad[i] -= rb * bd[i]; ad[i] -= rc * cd[i]; } for (int i = 0; i < rb; i++) printf("%s", b); for (int i = 0; i < rc; i++) printf("%s", c); for (int i = 0; i < 26; i++) { for (int j = 0; j < ad[i]; j++) putchar('a' + i); } putchar('\n'); }
#include <bits/stdc++.h> using namespace std; template <class T, class X> inline bool checkBit(T a, X i) { T t = 1; return ((a & (t << i))); } template <class T, class X> inline T setBit(T a, X i) { T t = 1; return (a | (t << i)); } template <class T, class X> inline T resetBit(T a, X i) { T t = 1; return (a & (~(t << i))); } char a[100010], b[100010], c[100010]; int r[30], rb[30], rc[30]; bool check(int n) { for (int i = 0; i < 26; ++i) if ((long long)n * rb[i] > r[i]) return 0; return 1; } int main() { scanf("%s%s%s", a, b, c); for (int i = 0; a[i]; ++i) ++r[a[i] - 'a']; for (int i = 0; b[i]; ++i) ++rb[b[i] - 'a']; for (int i = 0; c[i]; ++i) ++rc[c[i] - 'a']; pair<int, int> res(0, 0); for (int i = 0; i <= 100000; ++i) { if (check(i)) { int tmp = 100000000; for (int j = 0; j < 26; ++j) if (rc[j]) tmp = min(tmp, (r[j] - rb[j] * i) / rc[j]); if (i + tmp > res.first + res.second) res = make_pair(i, tmp); } } for (int i = 0; i < res.first; ++i) printf("%s", b); for (int i = 0; i < res.second; ++i) printf("%s", c); for (int i = 0; i < 26; ++i) r[i] -= rb[i] * res.first + rc[i] * res.second; for (int i = 0; i < 26; ++i) for (int j = 0; j < r[i]; ++j) printf("%c", char(i + 'a')); printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; string a, b, c, str1, str2; int alp[4][29]; void setal() { memset(alp, 0, sizeof alp); for (int i = 0; i < a.length(); i++) alp[0][a[i] - 'a']++; for (int i = 0; i < b.length(); i++) alp[1][b[i] - 'a']++; for (int i = 0; i < c.length(); i++) alp[2][c[i] - 'a']++; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; while (cin >> a >> b >> c) { int A = 0, B = 0, m = 0; setal(); for (int i = 0; i <= 300005; i++) { int t = 10000000; bool f = true; for (int j = 0; j < 26; j++) { if (i * alp[1][j] > alp[0][j]) { f = false; break; } if (alp[2][j]) t = min((alp[0][j] - i * alp[1][j]) / alp[2][j], t); } if (f) { if (i + t > m) { m = i + t; A = i; B = t; } } else break; } while (A) { cout << b; for (int i = 0; i < 26; i++) alp[0][i] -= alp[1][i]; A--; } while (B) { cout << c; for (int i = 0; i < 26; i++) alp[0][i] -= alp[2][i]; B--; } for (int i = 0; i < 26; i++) while (alp[0][i]--) cout << (char)(i + 'a'); cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long a[26], b[26], c[26], c1, c2; int main() { string s1, s2, s3; cin >> s1 >> s2 >> s3; for (int i = 0; i < s1.size(); i++) { a[s1[i] - 'a']++; } for (int i = 0; i < s2.size(); i++) { b[s2[i] - 'a']++; } for (int i = 0; i < s3.size(); i++) { c[s3[i] - 'a']++; } while (1) { long long m = 1e9, m1 = 1e9; for (int i = 0; i < 26; i++) { if (b[i]) { m = min(m, a[i] / b[i]); } if (c[i]) { m1 = min(m1, a[i] / c[i]); } } if (m == 0 && m1 == 0) { break; } if (m > m1) { c1++; for (int i = 0; i < 26; i++) { a[i] -= b[i]; } } else { c2++; for (int i = 0; i < 26; i++) { a[i] -= c[i]; } } } while (c1--) { cout << s2; } while (c2--) { cout << s3; } for (int i = 0; i < 26; i++) { while (a[i]--) { cout << (char)(i + 'a'); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 100100; const int ALPHA = 26; const int oo = 2 * MAXN; int ord(char c) { return c - 'a'; } char chr(int o) { return 'a' + o; } string s[3]; int f[3][ALPHA], sol, sol_b, sol_c; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> s[0] >> s[1] >> s[2]; for (int k = 0; k < 3; k++) for (int i = 0; i < (int)s[k].length(); i++) f[k][ord(s[k][i])]++; sol = -1; int cant_b = 0; while (true) { bool ok = true; for (int i = 0; i < ALPHA; i++) if (cant_b * f[1][i] > f[0][i]) { ok = false; break; } if (!ok) break; int cant_c = oo; for (int i = 0; i < ALPHA; i++) if (f[2][i] > 0) cant_c = min(cant_c, (f[0][i] - cant_b * f[1][i]) / f[2][i]); if (sol < cant_b + cant_c) { sol = cant_b + cant_c; sol_b = cant_b; sol_c = cant_c; } cant_b++; } for (int i = 0; i < sol_b; i++) cout << s[1]; for (int i = 0; i < sol_c; i++) cout << s[2]; for (int i = 0; i < ALPHA; i++) { int left = f[0][i] - sol_b * f[1][i] - sol_c * f[2][i]; for (int j = 0; j < left; j++) cout << chr(i); } cout << endl; }
#include <bits/stdc++.h> #pragma optimization_level 3 using namespace std; using ll = long long; using ld = long double; const ll MOD = 1e+9 + 7; const ll INF = INT_MAX; const int N = (int)2e+5 + 8; string second, a, b; ll cnts[30], cntss[30], cnta[30], cntb[30]; void MAIN(ll tc) { cin >> second >> a >> b; string ca = a, cb = b; ll n = second.size(), na = a.size(), nb = b.size(); second = "#" + second + "#"; ; a = "#" + a + "#"; ; b = "#" + b + "#"; ; for (ll i = 1; i <= n; i++) { cnts[second[i] - 'a' + 1]++; cntss[second[i] - 'a' + 1]++; } for (ll i = 1; i <= na; i++) { cnta[a[i] - 'a' + 1]++; } for (ll i = 1; i <= nb; i++) { cntb[b[i] - 'a' + 1]++; } ll ans = 0, geta = INF, getb = INF; for (int i = 1; i <= n; i++) { ll x = INF; for (ll i = 1; i <= 26; i++) { if (cnta[i] == 0) continue; x = min(x, cnts[i] / cnta[i]); } if (x == INF) x = 0; if (ans < i - 1 + x) { ans = i - 1 + x; geta = x; getb = i - 1; } int ok = 0; for (ll i = 1; i <= 26; i++) { if (cntb[i] == 0) continue; cnts[i] -= cntb[i]; if (cnts[i] < 0) { ok = 1; } } if (ok) break; } if (geta == INF) geta = 0; if (getb == INF) getb = 0; string res = ""; for (ll i = 1; i <= geta; i++) res += ca; for (ll i = 1; i <= getb; i++) res += cb; for (ll i = 1; i <= 26; i++) { cntss[i] -= (geta * cnta[i] + getb * cntb[i]); res += string(cntss[i], char(i + 'a' - 1)); } cout << res << "\n"; ; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout << fixed; cout << setprecision(10); int test_cases = 1; for (int i = 1; i <= test_cases; i++) { MAIN(i); } }
#include <bits/stdc++.h> using namespace std; const int N = 123456; int sa[30], sb[30], sc[30]; string a, b, c; int ans = 0, n, m; int main() { cin >> a >> b >> c; for (int i = 0; i < a.size(); ++i) { sa[a[i] - 'a']++; } for (int i = 0; i < b.size(); ++i) { sb[b[i] - 'a']++; } for (int i = 0; i < c.size(); ++i) { sc[c[i] - 'a']++; } for (int i = 0; i <= a.size(); ++i) { int tem = -1, k = 2000000; for (int j = 0; j < 26; ++j) { if (i * sb[j] <= sa[j]) { if (sc[j] > 0) { k = min(k, (sa[j] - i * sb[j]) / sc[j]); tem = k + i; } } else { tem = -1; break; } } if (tem > ans) { ans = tem; n = i, m = k; } if (tem == -1) break; } for (int i = 0; i < 26; ++i) sa[i] -= sb[i] * n + sc[i] * m; for (int i = 1; i <= n; ++i) cout << b; for (int i = 1; i <= m; ++i) cout << c; for (int i = 0; i < 26; ++i) { for (int j = 1; j <= sa[i]; ++j) { putchar('a' + i); } } }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; char s1[100005], s2[100005], s3[100005]; int len1, len2, len3; int h1[30], h2[30], h3[30]; int cnt2, cnt3, cnt; int main() { int i, j, k; while (~scanf("%s%s%s", s1, s2, s3)) { len1 = strlen(s1); len2 = strlen(s2); len3 = strlen(s3); memset(h1, 0, sizeof(h1)); memset(h2, 0, sizeof(h2)); memset(h3, 0, sizeof(h3)); for (i = 0; i < len1; i++) h1[s1[i] - 'a']++; for (i = 0; i < len2; i++) h2[s2[i] - 'a']++; for (i = 0; i < len3; i++) h3[s3[i] - 'a']++; cnt = 0; for (i = 1;; i++) { int p = 100005; for (j = 0; j < 26; j++) { if (h1[j] - h2[j] * i < 0) { p = 0; break; } } if (!p) break; int p2 = 100005; for (j = 0; j < 26; j++) { if (h3[j]) { p2 = min(p2, (h1[j] - h2[j] * i) / h3[j]); } } if (i + p2 > cnt) { cnt = i + p2; cnt2 = i; cnt3 = p2; } } for (i = 1;; i++) { int p = 100005; for (j = 0; j < 26; j++) { if (h1[j] - h3[j] * i < 0) { p = 0; break; } } if (!p) break; int p2 = 100005; for (j = 0; j < 26; j++) { if (h2[j]) { p2 = min(p2, (h1[j] - h3[j] * i) / h2[j]); } } if (i + p2 > cnt) { cnt = i + p2; cnt3 = i; cnt2 = p2; } } for (i = 0; i < cnt2; i++) printf("%s", s2); for (i = 0; i < cnt3; i++) printf("%s", s3); for (i = 0; i < 26; i++) { for (j = 0; j < h1[i] - cnt2 * h2[i] - cnt3 * h3[i]; j++) printf("%c", i + 'a'); } printf("\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; char a[100100], b[100100], c[100100]; int cnt[26], cnt1[26], cnt2[26]; int b1, c1, ans1, ans2, sum; int main() { scanf("%s%s%s", a, b, c); for (int i = 0; a[i]; ++i) ++cnt[a[i] - 'a']; for (int i = 0; b[i]; ++i) ++cnt1[b[i] - 'a']; for (int i = 0; c[i]; ++i) ++cnt2[c[i] - 'a']; b1 = c1 = 1e9, sum = 0; for (int i = 0; i < 26; ++i) if (cnt1[i]) b1 = min(b1, cnt[i] / cnt1[i]); for (int i = 0; i < 26; ++i) if (cnt2[i]) c1 = min(c1, cnt[i] / cnt2[i]); for (int i = 0; i <= b1; ++i) { for (int j = 0; j < 26; ++j) if (cnt2[j]) c1 = min(c1, (cnt[j] - i * cnt1[j]) / cnt2[j]); if (i + c1 > sum) sum = i + c1, ans1 = i, ans2 = c1; } 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 < 26; ++i) cnt[i] -= ans1 * cnt1[i] + ans2 * cnt2[i]; for (int i = 0; i < 26; ++i) while (cnt[i]--) putchar(i + 'a'); return 0; }
#include <bits/stdc++.h> using namespace std; char a[100005], b[100005], c[100005]; int ca[30], cb[30], cc[30]; int main() { scanf("%s%s%s", a, b, c); int la = strlen(a); int lb = strlen(b); int lc = strlen(c); int i; for (i = 0; i < la; i++) ca[a[i] - 'a']++; for (i = 0; i < lb; i++) cb[b[i] - 'a']++; for (i = 0; i < lc; i++) cc[c[i] - 'a']++; int nb = 999999999, nc = 999999999; int mx = 0, ansb = 0, ansc = 0; for (i = 0; i < la; i++) { bool flag = 0; nc = 999999999; for (int j = 0; j < 26; j++) { if (i * cb[j] > ca[j]) { flag = 1; break; } if (cc[j]) nc = min(nc, (ca[j] - i * cb[j]) / cc[j]); } if (flag) break; nb = i; if (nc + nb > mx) { mx = nc + nb; ansb = nb; ansc = nc; } } for (i = 0; i < 26; i++) { ca[i] -= ansb * cb[i] + ansc * cc[i]; } while (ansb--) { printf("%s", b); } while (ansc--) { printf("%s", c); } for (i = 0; i < 26; i++) { while (ca[i]--) { printf("%c", 'a' + i); } } return 0; }
#include <bits/stdc++.h> using namespace std; int s[26]; int s1[26]; int s2[26]; const int maxn = 100100; char ss[maxn], ss1[maxn], ss2[maxn]; void getnum(int s[], char* s3) { int l = strlen(s3); for (int i = 0; i < l; i++) s[s3[i] - 'a']++; } int main() { cin >> ss >> ss1 >> ss2; getnum(s, ss); getnum(s1, ss1); getnum(s2, ss2); int n1 = maxn, n2 = maxn; for (int i = 0; i < 26; i++) { if (s[i] == 0 && s1[i]) { n1 = 0; break; } if (s[i] && s1[i] && s[i] / s1[i] < n1) n1 = s[i] / s1[i]; } int ans1 = n1, ans2 = 0; for (int i = 0; i <= n1; i++) { n2 = maxn; for (int j = 0; j < 26; j++) { int t = s[j] - i * s1[j]; if (s2[j] && t / s2[j] < n2) { n2 = t / s2[j]; } } if (n2 + i > ans1 + ans2) { ans1 = i; ans2 = n2; } } for (int i = 0; i < ans1; i++) cout << ss1; for (int i = 0; i < ans2; i++) cout << ss2; for (int i = 0; i < 26; i++) { int n = s[i] - ans1 * s1[i] - ans2 * s2[i]; char p = i + 'a'; for (int j = 0; j < n; j++) { cout << p; } } cout << endl; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; template <typename T, typename TT> inline ostream &operator<<(ostream &os, const pair<T, TT> &t) { return os << t.first << " " << t.second; } template <typename T> inline ostream &operator<<(ostream &os, const vector<T> &t) { for (auto i : t) os << i << " "; return os; } template <typename T> inline ostream &operator<<(ostream &os, const set<T> &t) { for (auto i : t) os << i << " "; return os; } template <typename T1, typename T2> inline ostream &operator<<(ostream &os, const map<T1, T2> &t) { for (auto i : t) os << i.first << " : " << i.second << '\n'; return os; } template <typename T> inline istream &operator>>(istream &is, vector<T> &v) { for (T &t : v) is >> t; return is; } template <typename T1, typename T2> inline istream &operator>>(istream &is, vector<pair<T1, T2>> &v) { for (pair<T1, T2> &t : v) is >> t.first >> t.second; return is; } const long long mod = 1e9 + 7; vector<long long> scnt(string &s) { vector<long long> cnt(26, 0); for (int i = 0; i < s.size(); i++) cnt[s[i] - 'a']++; return cnt; } void solve() { string s, a, b; cin >> s >> a >> b; vector<long long> cnt1 = scnt(s), cnt2 = scnt(a), cnt3 = scnt(b); long long mx = 1e18; for (int i = 0; i < 26; i++) { if (cnt2[i]) mx = min(mx, cnt1[i] / cnt2[i]); } pair<long long, long long> req = {0, 0}; for (int i = 0; i <= mx; i++) { long long c = 1e18; for (int j = 0; j < 26; j++) { if (cnt3[j]) c = min(c, (cnt1[j] - i * cnt2[j]) / cnt3[j]); } if (i + c > req.first + req.second) req = {i, c}; }; string ans = ""; for (int i = 0; i < 26; i++) cnt1[i] -= req.first * cnt2[i] + req.second * cnt3[i]; while (req.first--) ans += a; while (req.second--) ans += b; for (int i = 0; i < 26; i++) while (cnt1[i]--) ans += char('a' + i); cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ; long long T = 1; while (T--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int lena, lenb, lenc, ans, ans1, ans2; char a[100010], b[100010], c[100010]; int cha[100], chb[100], chc[100]; int main() { scanf("%s", a); scanf("%s", b); scanf("%s", c); lena = strlen(a); lenb = strlen(b); lenc = strlen(c); for (int i = 0; i < lena; i++) { cha[a[i] - 'a']++; } for (int i = 0; i < lenb; i++) { chb[b[i] - 'a']++; } for (int i = 0; i < lenc; i++) { chc[c[i] - 'a']++; } for (int i = 0;; i++) { int temp = 200010; bool flag = true; for (int j = 0; j < 26; j++) if (chb[j] * i > cha[j]) { flag = false; break; } if (!flag) break; for (int j = 0; j < 26; j++) if (chc[j]) temp = min(temp, (cha[j] - chb[j] * i) / chc[j]); if (temp + i > ans) { ans = temp + i; ans1 = i; ans2 = temp; } } for (int i = 1; i <= ans1; i++) printf("%s", b); for (int i = 1; i <= ans2; i++) printf("%s", c); for (int i = 0; i < 26; i++) for (int j = 1; j <= cha[i] - chb[i] * ans1 - chc[i] * ans2; j++) printf("%c", 'a' + i); printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; int generateb(int a, long int nos[], long int noa[], long int nob[]) { int i, minb = 100002; for (i = 0; i < 26; i++) { nos[i] = nos[i] - (noa[i] * a); } for (i = 0; i < 26; i++) { if (nob[i] != 0) { if (nos[i] / nob[i] < minb) { minb = nos[i] / nob[i]; } } } return minb; } int generatea(int b, long int nos[], long int noa[], long int nob[]) { int i, mina = 100002; for (i = 0; i < 26; i++) { nos[i] = nos[i] - (nob[i] * b); } for (i = 0; i < 26; i++) { if (noa[i] != 0) { if (nos[i] / noa[i] < mina) { mina = nos[i] / noa[i]; } } } return mina; } int main() { char s[100010], a[100010], b[100010]; long int nos[26] = {0}, i, lens = 0, noa[26] = {0}, nob[26] = {0}, lena = 0, lenb = 0, j, nos1[26] = {0}, mina = 100002, counta, countb, max = -1, maxa, maxb, minb = 100002, r; cin >> s >> a >> b; for (i = 0; s[i] != '\0'; i++) { nos[s[i] - 'a']++; lens++; } for (j = 0; j < 26; j++) { nos1[j] = nos[j]; } for (i = 0; a[i] != '\0'; i++) { noa[a[i] - 'a']++; lena++; } for (i = 0; b[i] != '\0'; i++) { nob[b[i] - 'a']++; lenb++; } for (i = 0; i < 26; i++) { if (noa[i] != 0) { if (nos[i] / noa[i] < mina) { mina = nos[i] / noa[i]; } } } for (i = 0; i <= mina; i++) { countb = generateb(i, nos, noa, nob); for (j = 0; j < 26; j++) { nos[j] = nos1[j]; } if (i + countb > max) { max = i + countb; maxa = i; maxb = countb; } } for (i = 0; i < 26; i++) { if (nob[i] != 0) { if (nos[i] / nob[i] < minb) { minb = nos[i] / nob[i]; } } } for (i = 0; i <= minb; i++) { counta = generatea(i, nos, noa, nob); for (j = 0; j < 26; j++) { nos[j] = nos1[j]; } if (i + counta > max) { max = i + counta; maxb = i; maxa = counta; } } for (i = 0; i < maxa; i++) { cout << a; } for (i = 0; i < maxb; i++) { cout << b; } for (i = 0; i < 26; i++) { nos[i] = nos[i] - (maxa * noa[i]) - (maxb * nob[i]); } for (i = 0; i < 26; i++) { for (j = 0; j < nos[i]; j++) { cout << char(i + 'a'); } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string a, b, c; cin >> a >> b >> c; int freq[3][26] = {}; for (int i = 0; i < a.length(); i++) freq[0][a[i] - 'a']++; for (int i = 0; i < b.length(); i++) freq[1][b[i] - 'a']++; for (int i = 0; i < c.length(); i++) freq[2][c[i] - 'a']++; int ans = 0, f = 0, s = 0; for (int t = 0; t < 100001; t++) { bool can = true; for (int j = 0; j < 26; j++) { if (freq[1][j] > 0) { can = can && (long long)t * freq[1][j] <= freq[0][j]; } } if (!can) break; long long mn = 1e6; for (int j = 0; j < 26; j++) { if (freq[2][j] > 0) { mn = min(mn, (freq[0][j] - (long long)t * freq[1][j]) / freq[2][j]); } } if (ans < mn + t) { ans = mn + t; f = t; s = mn; } } string res = ""; for (int j = 0; j < 26; j++) { freq[0][j] -= f * freq[1][j]; freq[0][j] -= s * freq[2][j]; } for (int i = 0; i < f; i++) { res += b; } for (int i = 0; i < s; i++) { res += c; } for (int i = 0; i < 26; i++) { for (int j = 0; j < freq[0][i]; j++) { res += ('a' + i); } } cout << res << endl; }
#include <bits/stdc++.h> int main() { int64_t n = 0; std::string a; std::string b; std::string c; std::cin >> a >> b >> c; uint64_t map_a['z' - 'a' + 1] = {0}; uint64_t map_b['z' - 'a' + 1] = {0}; uint64_t map_c['z' - 'a' + 1] = {0}; for (std::string::iterator it = a.begin(); it != a.end(); ++it) { map_a[*it - 'a']++; } for (std::string::iterator it = b.begin(); it != b.end(); ++it) { map_b[*it - 'a']++; } for (std::string::iterator it = c.begin(); it != c.end(); ++it) { map_c[*it - 'a']++; } char map[] = "abcdefghijklmnopqrstuvwxyz"; uint64_t max_b = 100000; uint64_t max_c = 100000; for (uint64_t i = 0; i < 'z' - 'a' + 1; ++i) { if (map_a[i] < map_b[i] * max_b) { max_b = map_a[i] / map_b[i]; } if (map_a[i] < map_c[i] * max_c) { max_c = map_a[i] / map_c[i]; } } int64_t max = 0; int64_t f_max_b = 0; int64_t f_max_c = 0; for (uint64_t ib = 0; ib <= max_b; ++ib) { int64_t cur_max_c = 100000; for (uint64_t i = 0; i < 'z' - 'a' + 1; ++i) { if ((map_a[i] - ib * map_b[i]) < map_c[i] * cur_max_c) { cur_max_c = (map_a[i] - ib * map_b[i]) / map_c[i]; } } if (cur_max_c + ib > max) { max = cur_max_c + ib; f_max_b = ib; f_max_c = cur_max_c; } } for (uint64_t ic = 0; ic <= max_c; ++ic) { int64_t cur_max_b = 100000; for (uint64_t i = 0; i < 'z' - 'a' + 1; ++i) { if ((map_a[i] - ic * map_c[i]) < map_b[i] * cur_max_b) { cur_max_b = (map_a[i] - ic * map_c[i]) / map_b[i]; } } if (cur_max_b + ic > max) { max = cur_max_b + ic; f_max_b = cur_max_b; f_max_c = ic; } } if (f_max_b == 0 && f_max_c == 0) { std::cout << a; } else { for (uint64_t i = 0; i < f_max_b; ++i) { std::cout << b; } for (uint64_t i = 0; i < f_max_c; ++i) { std::cout << c; } for (uint64_t i = 0; i < 'z' - 'a' + 1; ++i) { uint64_t left = map_a[i] - f_max_b * map_b[i] - f_max_c * map_c[i]; for (uint64_t j = 0; j < left; ++j) { std::cout << map[i]; } } } std::cout << std::endl; return 0; }
#include <bits/stdc++.h> using namespace std; int c1[200]; int c2[200]; int c3[200]; int main() { string a, b, c; cin >> a >> b >> c; for (int i = 0; i < 200; i++) c1[i] = c2[i] = c3[i] = 0; for (int i = 0; i < a.size(); i++) c1[a[i]]++; for (int i = 0; i < b.size(); i++) c2[b[i]]++; for (int i = 0; i < c.size(); i++) c3[c[i]]++; int aux[200]; for (int i = 'a'; i <= 'z'; i++) { aux[i] = c1[i]; } int t1 = 0, t2 = 0; int ans1 = 0, ans2 = 0; int best = 0; bool stop = false; while (true) { t2 = INT_MAX; for (int i = 'a'; i <= 'z'; i++) { if (c3[i] != 0) t2 = min(t2, aux[i] / c3[i]); } if (t1 + t2 > ans1 + ans2) { ans1 = t1; ans2 = t2; } for (int i = 'a'; i <= 'z'; i++) { if (c2[i] > aux[i]) { stop = true; break; } aux[i] -= c2[i]; } if (stop) break; t1++; } for (int i = 'a'; i <= 'z'; i++) { c1[i] -= ans1 * c2[i]; c1[i] -= ans2 * c3[i]; for (int k = 0; k < c1[i]; k++) printf("%c", (char)i); } for (int i = 0; i < ans1; i++) cout << b; for (int i = 0; i < ans2; i++) cout << c; cout << endl; }
#include <bits/stdc++.h> using namespace std; string s[3]; int cnt[3][26]; string convert(int times[]) { string ans; for (int i = 1; i < 3; ++i) { for (int j = 0; j < times[i]; ++j) ans += s[i]; for (int j = 0; j < 26; ++j) cnt[0][j] -= cnt[i][j] * times[i]; } for (int i = 0; i < 26; ++i) for (int j = 0; j < cnt[0][i]; ++j) ans += i + 'a'; return ans; } int main() { cin >> s[0] >> s[1] >> s[2]; for (int i = 0; i < 3; ++i) for (int j = 0; j < s[i].size(); ++j) cnt[i][s[i][j] - 'a']++; int cost[26] = {}; int best = 0, times[3] = {}; for (int b = 0; b <= s[0].size(); ++b) { if (b != 0) for (int i = 0; i < 26; ++i) cost[i] += cnt[1][i]; int left[26]; bool too_much = false; for (int i = 0; i < 26; ++i) { left[i] = cnt[0][i] - cost[i]; if (left[i] < 0) too_much = true; } if (too_much) break; int c = s[0].size(); for (int i = 0; i < 26; ++i) if (cnt[2][i] != 0) c = min(c, left[i] / cnt[2][i]); if (b + c > best) { best = b + c; times[1] = b; times[2] = c; } } cout << convert(times) << '\n'; };
#include <bits/stdc++.h> using namespace std; const long long int M = 1e9 + 7; long long int inf = 1e18; long long int n, m; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long int i, j, t, k, x, y, z, N; string a, b, c; cin >> a >> b >> c; map<char, long long int> A, B, C; for (i = 0; i < a.length(); i++) { A[a[i]]++; } for (i = 0; i < b.length(); i++) { B[b[i]]++; } for (i = 0; i < c.length(); i++) { C[c[i]]++; } long long int mn1 = inf, mn2 = inf; for (auto u : B) { x = A[u.first] / u.second; mn1 = min(mn1, x); } long long int mx = 0, p1 = 0, p2 = 0; for (i = 0; i <= mn1; i++) { map<char, long long int> dm = A; for (auto u : B) { dm[u.first] -= i * u.second; } long long int mn2 = inf; for (auto u : C) { x = dm[u.first] / u.second; mn2 = min(mn2, x); } if (i + mn2 > mx) { mx = i + mn2; p1 = i; p2 = mn2; } } for (i = 0; i < p1; i++) cout << b; for (i = 0; i < p2; i++) cout << c; for (auto u : B) { A[u.first] -= p1 * u.second; } for (auto u : C) { A[u.first] -= p2 * u.second; } for (auto u : A) { for (i = 0; i < u.second; i++) cout << u.first; } return 0; }
#include <bits/stdc++.h> using namespace std; long long bin_pow(long long a, long long b) { if (!b) return 1; long long ans = bin_pow(a, b / 2); ans = ans * ans; if (b % 2) ans = ans * a; return ans; } bool comp(int a, int b) { if (a > b) return true; return false; } int nums[3][26]; int main() { string s; string s1[2]; cin >> s; cin.ignore(); cin >> s1[0]; cin.ignore(); cin >> s1[1]; int i, j; for (i = 0; i < s.size(); i++) { nums[0][s[i] - 'a']++; } for (i = 0; i < s1[0].size(); i++) { nums[1][s1[0][i] - 'a']++; } for (i = 0; i < s1[1].size(); i++) { nums[2][s1[1][i] - 'a']++; } int min1 = INT_MAX; for (i = 0; i < 26; i++) { if (nums[1][i] != 0) { min1 = min(min1, nums[0][i] / nums[1][i]); } } int ans1 = 0; int ans2 = 0; for (i = 0; i <= min1; i++) { int min2 = INT_MAX; for (j = 0; j < 26; j++) { if (nums[2][j] != 0) { min2 = min(min2, (nums[0][j] - nums[1][j] * i) / nums[2][j]); } } if (i + min2 > ans1 + ans2) { ans1 = i; ans2 = min2; } } for (i = 0; i < ans1; i++) { cout << s1[0]; } for (i = 0; i < ans2; i++) { cout << s1[1]; } for (i = 0; i < 26; i++) { for (j = 0; j < nums[0][i] - nums[1][i] * ans1 - nums[2][i] * ans2; j++) { cout << (char)(i + 'a'); } } return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:66777216") #pragma warning(disable : 4996) #pragma hdrstop using namespace std; const int INF = 0x3f3f3f3f; template <class T> inline bool umin(T& a, const T& b) { return (b < a ? a = b, true : false); } class IO { public: static const int BUFFER_SIZE = 1 << 18; bool eof; char curChar; IO() : rbCount(0), rbOffset(0), curChar(0), eof(false) {} ~IO() { fwrite(writeBuffer, sizeof(writeBuffer[0]), wbOffset, stdout); } inline void nextString(char* s); IO& operator>>(char* s); void newline(); void printString(const char* s); IO& operator<<(const char* s); IO& operator<<(ostream& (*fn)(ostream&)); private: char readBuffer[BUFFER_SIZE], writeBuffer[BUFFER_SIZE]; size_t rbCount, rbOffset, wbOffset; }; extern bool useFastIO; extern istream* pin; extern ostream* pout; extern IO io; inline void nextString(char* s) { if (useFastIO) { io.nextString(s); return; } *pin >> s; } inline void newline() { if (useFastIO) { io.newline(); return; } *pout << endl; } inline void printString(const char* s) { if (useFastIO) { io.printString(s); return; } *pout << s; } IO io; const int MAXN = 111111; struct Str { int cnt[255], ls; char s[MAXN]; Str() {} void read() { io >> s; ls = strlen(s); memset(cnt, 0, sizeof(cnt)); for (int i = 0; i < ls; ++i) { ++cnt[s[i]]; } } } a, b, c; int cc; char ans[MAXN]; void solve(istream& ins, ostream& out) { a.read(); b.read(); c.read(); cc = 0; for (;;) { int mxa = INF, mxb = INF; for (int i = 'a'; i <= 'z'; ++i) { if (b.cnt[i]) { umin(mxa, a.cnt[i] / b.cnt[i]); } } for (int i = 'a'; i <= 'z'; ++i) { if (c.cnt[i]) { umin(mxb, a.cnt[i] / c.cnt[i]); } } if (!mxa && !mxb) { break; } Str& cur = (mxa < mxb ? c : b); for (int j = 0; j < cur.ls; ++j) { char ch = cur.s[j]; ans[cc++] = cur.s[j]; --a.cnt[ch]; } } for (int i = 'a'; i <= 'z'; ++i) { for (int j = 0; j < a.cnt[i]; ++j) { ans[cc++] = i; } } ans[cc] = 0; io << ans << endl; } extern class IO io; bool useFastIO = false; istream* pin; ostream* pout; int main() { srand(time(NULL)); ios_base::sync_with_stdio(0); cin.tie(0); istream& in = cin; useFastIO = true; ostream& out = cout; out << fixed << setprecision(16); pin = &in; pout = &out; solve(in, out); return 0; } namespace Random {} inline void IO::nextString(char* s) { if (!curChar) { rbCount = fread(readBuffer, sizeof(readBuffer[0]), sizeof(readBuffer), stdin); if (rbCount <= 0) { eof = true; return; } rbOffset = 0; curChar = readBuffer[rbOffset++]; } while (!eof && (curChar == ' ' || curChar == '\n')) { if (rbOffset == rbCount) { rbCount = fread(readBuffer, sizeof(readBuffer[0]), sizeof(readBuffer), stdin); if (rbCount <= 0) { eof = true; return; } rbOffset = 0; } curChar = readBuffer[rbOffset++]; } int ind = 0; while (!eof && curChar != ' ' && curChar != '\n') { s[ind++] = curChar; if (rbOffset == rbCount) { rbCount = fread(readBuffer, sizeof(readBuffer[0]), sizeof(readBuffer), stdin); if (rbCount <= 0) { eof = true; s[ind] = 0; return; } rbOffset = 0; } curChar = readBuffer[rbOffset++]; } s[ind] = 0; } IO& IO::operator>>(char* s) { ::nextString(s); return *this; } void IO::newline() { if (wbOffset == BUFFER_SIZE) { fwrite(writeBuffer, sizeof(writeBuffer[0]), wbOffset, stdout); wbOffset = 0; } writeBuffer[wbOffset++] = '\n'; } void IO::printString(const char* s) { for (int i = 0; s[i]; ++i) { if (wbOffset == BUFFER_SIZE) { fwrite(writeBuffer, sizeof(writeBuffer[0]), wbOffset, stdout); wbOffset = 0; } writeBuffer[wbOffset++] = s[i]; } } IO& IO::operator<<(const char* s) { ::printString(s); return *this; } IO& IO::operator<<(ostream& (*fn)(ostream&)) { ::newline(); return *this; }
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const int MAXN = 100005; string a, b, c; int cntA[128], cntB[128], cntC[128]; void count(string &s, int cnt[128]) { for (int i = 0; i < 128; i++) { cnt[i] = 0; } for (size_t i = 0; i < s.size(); i++) { cnt[(int)s[i]]++; } } void solve() { count(a, cntA); count(b, cntB); count(c, cntC); int memB = 0, memC = 0; bool good = true; for (int numB = 0; numB <= (int)(a.size() / b.size()) && good; numB++) { int numC = MAXN; for (int x = 'a'; x <= 'z'; x++) { int numX = cntB[x] * numB; if (numX > cntA[x]) { good = false; break; } if (cntC[x]) { numC = min(numC, (cntA[x] - numX) / cntC[x]); } } if (good && numB + numC > memB + memC) { memB = numB; memC = numC; } } for (int x = 'a'; x <= 'z'; x++) { cntA[x] -= memB * cntB[x] + memC * cntC[x]; } for (int i = 0; i < memB; i++) { cout << b; } for (int i = 0; i < memC; i++) { cout << c; } for (int x = 'a'; x <= 'z'; x++) { for (int i = 0; i < cntA[x]; i++) { cout << char(x); } } cout << endl; } int main() { ios_base::sync_with_stdio(false); while (cin >> a >> b >> c) { solve(); } }