spider68

string subsequence

Jul 24th, 2020
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.    
  4. // str : Stores input string
  5. // n : Length of str.
  6. // curr : Stores current permutation
  7. // index : Index in current permutation, curr
  8. void printSubSeqRec(string str, int n,
  9.            int index = -1, string curr = "")
  10. {
  11.     // base case
  12.     if (index == n)  
  13.         return;
  14.    
  15.     cout << curr << "\n";
  16.     for (int i = index + 1; i < n; i++) {
  17.    
  18.         curr += str[i];
  19.         printSubSeqRec(str, n, i, curr);
  20.    
  21.         // backtracking
  22.         curr = curr.erase(curr.size() - 1);  
  23.     }
  24.     return;
  25. }
  26.    
  27. // Generates power set in lexicographic
  28. // order.
  29. void printSubSeq(string str)
  30. {
  31.     printSubSeqRec(str, str.size());
  32. }
  33.    
  34. // Driver code
  35. int main()
  36. {
  37.     string str = "cab";
  38.     printSubSeq(str);
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment