fork download
  1. #include<bits/stdc++.h>
  2. #define size 100000
  3. using namespace std;
  4. main()
  5. {
  6. char a[size+1],b[size+1],c[size+1];
  7. // freopen("in.txt","r",stdin);
  8. cin>>a>>b>>c;
  9. int h1[26],h2[26],h3[26],l1,l2,l3;
  10. l1=strlen(a);
  11. l2=strlen(b);
  12. l3=strlen(c);
  13. memset(h1,0,sizeof(h1));
  14. memset(h2,0,sizeof(h2));
  15. memset(h3,0,sizeof(h3));
  16. for(int i=0;i<l1;++i)
  17. ++h1[a[i]-'a'];
  18. for(int i=0;i<l2;++i)
  19. ++h2[b[i]-'a'];
  20. for(int i=0;i<l3;++i)
  21. ++h3[c[i]-'a'];
  22. int cur=0,val,ans=INT_MAX,ma=0,b1=0,c1=0,flag;
  23. for(int i=0;i<l1;++i)
  24. {
  25. cur=i;
  26.  
  27. flag=0;
  28. for(int j=0;j<26;++j)
  29. {
  30. if(h1[j]-(cur*h2[j])<0)
  31. {
  32. flag=1;
  33. break;
  34. }
  35. }
  36. if(flag==0)
  37. {
  38. ans=INT_MAX;
  39. for(int j=0;j<26;++j)
  40. {
  41. if(h3[j]!=0)
  42. {
  43. val=(h1[j]-(h2[j]*cur))/h3[j];
  44. if(val<=0)
  45. val=0;
  46. ans=min(ans,val);
  47. }
  48. }
  49. if(ma<cur+ans)
  50. {
  51. ma=cur+ans;
  52. b1=cur;
  53. c1=ans;
  54. }
  55. }
  56. }
  57. int cur1=0,ma1=0,ans1=INT_MAX,b2=0,c2=0;
  58. for(int i=0;i<l1;++i)
  59. {
  60. cur1=i;
  61.  
  62. flag=0;
  63. for(int j=0;j<26;++j)
  64. {
  65. if(h1[j]-(cur*h3[j])<0)
  66. {
  67. flag=1;
  68. break;
  69. }
  70. }
  71. if(flag==0)
  72. {
  73. ans1=INT_MAX;
  74. for(int j=0;j<26;++j)
  75. {
  76. if(h2[j]!=0)
  77. {
  78. val=(h1[j]-(h3[j]*cur))/h2[j];
  79. if(val<=0)
  80. val=0;
  81. ans1=min(ans1,val);
  82. }
  83. }
  84. if(ma1<cur1+ans1)
  85. {
  86. ma1=cur1+ans1;
  87. b2=cur;
  88. c2=ans;
  89. }
  90. }
  91. }
  92. // cout<<ma<<endl;
  93. if(ma1>ma)
  94. {
  95. ma=ma1;
  96. cur=cur1;
  97. ans=ans1;
  98. b1=b2;
  99. c1=c2;
  100. }
  101. cur=ma;
  102. for(int i=0;i<26;++i)
  103. h1[i]-=(h2[i]*cur+h3[i]*ans);
  104. for(int i=0;i<cur;++i)
  105. cout<<b;
  106. for(int i=0;i<ans;++i)
  107. cout<<c;
  108. for(int i=0;i<26;++i)
  109. {
  110. for(int j=0;j<h1[i];++j)
  111. cout<<(char)(i+'a');
  112. }
  113. }
Success #stdin #stdout 0s 3312KB
stdin
lemigazalemiolemilicomzalemljenje
lemi
zlo
stdout
lemilemilemilemilemilemizlozloaaacgjjn