fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define PI 3.14159265359
  4. #define DP(x, y) memset(x, y, sizeof x)
  5. #define all(x) x.begin(), x.end()
  6. #define read(x) freopen("x", "r", stdin);
  7. #define write(x) freopen("x", "w", stdout);
  8.  
  9. using namespace std;
  10.  
  11. ll gcd(ll a,ll b) { while(b) { ll x = a; a = b; b = x % b; } return a; }
  12. ll lcm(ll a,ll b) { return a / gcd(a, b) * b; }
  13. ll nC2(ll n) { return (n)*(n-1)/2; }
  14. ll summing(ll n) { return (n)*(n+1); }
  15.  
  16. void Queue(int i, int j, int n, int m, vector<int>&a, vector<int>&b, vector<int>&temp, set<vector<int>>&ans) {
  17.  
  18. if (i == n && j == m) {
  19. ans.insert(temp);
  20. return;
  21. }
  22. if (i == n) {
  23. if (b[j] > temp.back()) {
  24. temp.push_back(b[j]);
  25. j++;
  26. Queue(i, j, n, m, a, b, temp, ans);
  27. temp.pop_back();
  28. j--;
  29. }
  30. else {
  31. ans.insert(temp);
  32. return;
  33. }
  34. }
  35. else if (j == m) {
  36. if (a[i] > temp.back()) {
  37. temp.push_back(a[i]);
  38. i++;
  39. Queue(i, j, n, m, a, b, temp, ans);
  40. temp.pop_back();
  41. i--;
  42. }
  43. else {
  44. ans.insert(temp);
  45. return;
  46. }
  47. }
  48.  
  49. if (a[i] > temp.back()) {
  50. temp.push_back(a[i]);
  51. i++;
  52. Queue(i, j, n, m, a, b, temp, ans);
  53. temp.pop_back();
  54. i--;
  55. }
  56.  
  57. if (b[j] > temp.back()) {
  58. temp.push_back(b[j]);
  59. j++;
  60. Queue(i, j, n, m, a, b, temp, ans);
  61. temp.pop_back();
  62. j--;
  63. }
  64.  
  65. }
  66.  
  67. void solve() {
  68.  
  69. int n,m;
  70.  
  71. cin >> n;
  72. vector<int> a(n);
  73. for (auto &i:a) cin >> i;
  74.  
  75. cin >> m;
  76. vector<int> b(m);
  77. for (auto &i:b) cin >> i;
  78.  
  79. vector<int> temp;
  80. temp.push_back(0);
  81. set<vector<int>> ans;
  82.  
  83. Queue(0, 0, n, m, a, b, temp, ans);
  84.  
  85. for (auto vec:ans) {
  86.  
  87. for (int i=1; i<vec.size(); i++) {
  88. cout << vec[i] << " ";
  89. }
  90. cout << "\n";
  91.  
  92. }
  93.  
  94. }
  95.  
  96. int main()
  97. {
  98. ios::sync_with_stdio(false);
  99. cin.tie(nullptr), cout.tie(nullptr);
  100.  
  101. ll t=1; /// cin >> t;
  102. while (t--) { solve(); }
  103.  
  104. return 0;
  105. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout