Advertisement
maycod23

Untitled

Jun 18th, 2022
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // bool recursivecheck(int curr, int end, vector<int>& v, int target, int &tempsum)
  5. // {
  6.  
  7. //  //base case
  8. //  if (tempsum == target) return true;
  9. //  if (curr == end) return false;
  10.  
  11. //  //let's include this
  12. //  tempsum += v[curr];
  13. //  if (recursivecheck(curr + 1, end, v, target, tempsum) == true) return true;
  14.  
  15. //  //let's exclude this
  16. //  tempsum -= v[curr];
  17. //  if (recursivecheck(curr + 1, end, v, target, tempsum) == true) return true;
  18.  
  19. //  return false;
  20. // }
  21.  
  22.  
  23. void printsubsets(int curr, int end, vector<int>& v, vector<int>& temp)
  24. {
  25.  
  26.     //base case
  27.     if (curr == end + 1)
  28.     {
  29.         if (temp.size() == 0) cout << "Empty Subset";
  30.         for (auto i : temp) cout << i << " ";
  31.         cout << endl;
  32.         return;
  33.     }
  34.  
  35.     //let's include this
  36.     temp.push_back(v[curr]);
  37.     printsubsets(curr + 1, end, v, temp);
  38.  
  39.     //let's exclude this
  40.     temp.pop_back();
  41.     printsubsets(curr + 1, end, v, temp);
  42.  
  43.     return;
  44. }
  45.  
  46.  
  47. void printuniquesubsets(int curr, int end, vector<int>& v, vector<int>& temp,
  48.                         set<vector<int>>& s)
  49. {
  50.  
  51.     //base case
  52.     if (curr == end + 1)
  53.     {
  54.         if (temp.size() == 0)
  55.         {
  56.             cout << "Empty Subset"; return ;
  57.         }
  58.         if (s.find(temp) != s.end()) return;//don't include this
  59.         else s.insert(temp);//include this
  60.         return;
  61.     }
  62.  
  63.     //let's include this
  64.     temp.push_back(v[curr]);
  65.     printuniquesubsets(curr + 1, end, v, temp, s);
  66.  
  67.     //let's exclude this
  68.     temp.pop_back();
  69.     printuniquesubsets(curr + 1, end, v, temp, s);
  70.  
  71.     return;
  72. }
  73. int main()
  74. {
  75.     // int n, target; cin >> n >> target;
  76.     // vector<int> v(n);
  77.     // for (int i = 0; i < n; i++) cin >> v[i];
  78.  
  79.     // int tempsum = 0;
  80.     // if (recursivecheck(0, n, v, target, tempsum))
  81.     // {
  82.     //  cout << "Found the subset" << endl;
  83.     // }
  84.     // else cout << "Not Found The subset" << endl;
  85.  
  86.  
  87.  
  88.     int n; cin >> n;
  89.     vector<int> v(n);
  90.     for (int i = 0; i < n; i++) cin >> v[i];
  91.     sort(v.begin(), v.end());
  92.  
  93.     vector<int> temp;
  94.     set<vector<int>> s;
  95.     // printsubsets(0, n - 1, v, temp);
  96.  
  97.     printuniquesubsets(0, n - 1, v, temp, s);
  98.  
  99.     for (auto i : s)
  100.     {
  101.         //i->vector
  102.         for (auto j : i) cout << j << " ";
  103.         cout << endl;
  104.     }
  105.  
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement