fork(12) download
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. const int N = 100010;
  8. int n, m, k, ba[26], bb[26], bs[26];
  9. int d, res, prvi, drugi;
  10. char s[N], a[N], b[N], resenje[N];
  11.  
  12. int main()
  13. {
  14. scanf("%s %s %s", s + 1, a + 1, b + 1);
  15. n = strlen(s + 1);
  16. m = strlen(a + 1);
  17. k = strlen(b + 1);
  18. for (int i = 1; i <= n; i++) bs[s[i] - 'a']++;
  19. for (int i = 1; i <= m; i++) ba[a[i] - 'a']++;
  20. for (int i = 1; i <= k; i++) bb[b[i] - 'a']++;
  21. for (int i = 0; ; i++)
  22. {
  23. int x = n;
  24. for (int j = 0; j < 26; j++) if (bb[j]) x = min(x, bs[j] / bb[j]);
  25. if (x + i > res)
  26. {
  27. res = x + i;
  28. prvi = i;
  29. drugi = x;
  30. }
  31. bool f = false;
  32. for (int j = 0; j < 26; j++) if (bs[j] < ba[j])
  33. {
  34. f = true;
  35. break;
  36. } else
  37. bs[j] -= ba[j];
  38. if (f) break;
  39. }
  40. for (int i = 1; i <= prvi; i++)
  41. for (int j = 1; j <= m; j++) resenje[++d] = a[j];
  42. for (int i = 1; i <= drugi; i++)
  43. for (int j = 1; j <= k; j++) resenje[++d] = b[j];
  44. for (int i = 0; i < 26; i++) bs[i] = -ba[i] * prvi - bb[i] * drugi;
  45. for (int i = 1; i <= n; i++) bs[s[i] - 'a']++;
  46. for (int i = 0; i < 26; i++)
  47. for (int j = 1; j <= bs[i]; j++) resenje[++d] = 'a' + i;
  48. for (int i = 1; i <= n; i++) printf("%c", resenje[i]);
  49. return 0;
  50. }
  51.  
Time limit exceeded #stdin #stdout 5s 3484KB
stdin
Standard input is empty
stdout
Standard output is empty