fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef pair<ll, ll > pii;
  6. typedef vector<int> vi;
  7. typedef vector<pii> vp;
  8. typedef vector<string> vs;
  9.  
  10.  
  11. #define all (v) ((v).begin()), ((v).end())
  12. #define sz(v) ((int)((v).size()))
  13. #define rep(i, n) for(ll i=0;i<(int)(n);++i)
  14. #define repi(i, j, n) for(ll i=(j);i<(int)(n);++i)
  15. #define repd(i, j, n) for(ll i=(j);i>=(int)(n);--i)
  16. #define repa(i, j, v) rep(i, sz(v)) rep(j, sz(v[0]))
  17. #define lp(i, cnt) for(ll i=0;i<(cnt);++i)
  18. #define REP(i,start,end) for(ll i = start ; i<end ; i++)
  19. #define eps 1.e-18
  20. #define MOD 1000000007
  21. set<string >result;
  22.  
  23. void BT(string s1, string s2, int n, string temp, int i, int j) {
  24. if (temp.size() == n) {
  25. result.insert(temp);
  26. return;
  27. }
  28. if (i < s1.size())
  29. BT(s1, s2, n, temp + s1[i], i + 1, j);
  30. if (j < s2.size())
  31. BT(s1, s2, n, temp + s2[j], i, j + 1);
  32.  
  33. }
  34.  
  35. int main() {
  36. ios_base::sync_with_stdio(false);
  37.  
  38. int t;
  39. cin>>t;
  40. while (t--) {
  41. result.clear() ;
  42. string s1, s2;
  43. cin >> s1>>s2;
  44. BT(s1, s2, s1.size() + s2.size(), "", 0, 0);
  45.  
  46.  
  47. for(set<string>::iterator t = result.begin() ; t!=result.end() ; t++){
  48. cout<<*t<<endl;
  49. }
  50. cout<<endl;
  51. }
  52.  
  53. }
Success #stdin #stdout 0s 3460KB
stdin
2 
a aa
ab cd 
stdout
aaa

abcd
acbd
acdb
cabd
cadb
cdab