Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. vector<vector<int>> weave(vector<int> lhs,
  7. vector<int> rhs,
  8. vector<int> prefix = vector<int>())
  9. {
  10. vector<vector<int>> output;
  11. if(rhs.empty())
  12. {
  13. auto single = prefix;
  14. single.insert(single.end(), lhs.begin(), lhs.end());
  15. output.push_back(single);
  16. }
  17. else if (lhs.empty())
  18. {
  19. auto single = prefix;
  20. single.insert(single.end(), rhs.begin(), rhs.end());
  21. output.push_back(single);
  22. }
  23. else
  24. {
  25. // LHS
  26. auto lhs_short = lhs;
  27. auto lhs_prefix = prefix;
  28. lhs_prefix.push_back(lhs_short.front());
  29. lhs_short.erase(lhs_short.begin());
  30. auto lhs_weaved = weave(lhs_short, rhs, lhs_prefix);
  31. output.insert(output.end(), lhs_weaved.begin(), lhs_weaved.end());
  32. // RHS
  33. auto rhs_short = rhs;
  34. auto rhs_prefix = prefix;
  35. rhs_prefix.push_back(rhs_short.front());
  36. rhs_short.erase(rhs_short.begin());
  37. auto rhs_weaved = weave(lhs, rhs_short, rhs_prefix);
  38. output.insert(output.end(), rhs_weaved.begin(), rhs_weaved.end());
  39. }
  40. return output;
  41. }
  42.  
  43.  
  44.  
  45.  
  46.  
  47. // To execute C++, please define "int main()"
  48. int main() {
  49.  
  50. vector<int> lhs = {1,3,5,7,9};
  51. vector<int> rhs = {0,2,4,6,8};
  52. auto out = weave(rhs,lhs);
  53. for(auto items: out)
  54. {
  55. cout << "{ ";
  56. for(auto item: items)
  57. {
  58. cout << item << " ";
  59. }
  60. cout << "}" << endl;
  61. }
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement