fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define all(x) x.begin(), x.end()
  4. #define f1(i, n) for(int i=1;i<=n;++i)
  5. using namespace std;
  6.  
  7. const int maxn = 2e6 + 1;
  8.  
  9. int n, x['Z'], cnt['Z'], ch[100];
  10. int n_old;
  11. vector<string> result;
  12.  
  13. string rev(string s) {
  14. string res = "";
  15. for (int i = s.size() - 1; i >= 0; --i) {
  16. res += s[i];
  17. }
  18. return res;
  19. }
  20.  
  21. bool check(string s) {
  22. for (int i = 0; i < s.size(); ++i) {
  23. if (cnt[s[i]] > x[s[i]]) {
  24. return false;
  25. }
  26. }
  27. return true;
  28. }
  29.  
  30. void ql(int i) {
  31. if (i > n) {
  32. string first_half = "";
  33. for (int i = 1; i <= n; ++i) {
  34. first_half += ch[i];
  35. cnt[ch[i]] += 2;
  36. }
  37.  
  38. string last_half = rev(first_half);
  39. if (n_old % 2 == 1) { // chữ ở giữa nếu n lẻ
  40. for (char c = 'A'; c <= 'D'; ++c) {
  41. if (cnt[c] > x[c]) {
  42. return;
  43. }
  44. if (cnt[c] + 1 > x[c]) {
  45. continue;
  46. }
  47. else {
  48. result.push_back(first_half + c + last_half);
  49. }
  50. }
  51. // nửa sau
  52. }
  53. else {
  54. for (char c = 'A'; c <= 'D'; ++c) {
  55. if (cnt[c] > x[c]) {
  56. return;
  57. }
  58. else {
  59. result.push_back(first_half + last_half);
  60. }
  61. }
  62. }
  63. return;
  64. }
  65.  
  66. ch[i] = 'A';
  67. ql(i + 1);
  68.  
  69. ch[i] = 'B';
  70. ql(i + 1);
  71.  
  72. ch[i] = 'C';
  73. ql(i + 1);
  74.  
  75. ch[i] = 'D';
  76. ql(i + 1);
  77.  
  78. }
  79.  
  80. int main() {
  81. ios::sync_with_stdio(false);
  82. cin.tie(nullptr);
  83.  
  84. cin >> n >> x['A'] >> x['B'] >> x['C'] >> x['D'];
  85.  
  86. n_old = n;
  87.  
  88. n /= 2;
  89.  
  90. ql(1);
  91.  
  92. for(auto x : result){
  93. cout<<x<<endl;
  94. }
  95.  
  96.  
  97.  
  98.  
  99. }
  100.  
Success #stdin #stdout 0s 5324KB
stdin
5 3 3 3 1
stdout
Standard output is empty