« Print subsequences of string

Problem

Given a string, we have to find out all subsequences of it. A String is a subsequence of a given String, that is generated by deleting some character of a given string without changing its order.

Input Format:

A string

Output Format:

Print all the subsequences of a string in different lines

Sample Input :

abc

Sample Output :

""

a

b

c

ab

bc

ac

abc

Solution

1#include <iostream>
2#include <iomanip>
3#include <algorithm>
4#include <string>
5#include <cstring>
6#include <vector>
7#include <cmath>
8#include <map>
9#include <climits>
10// climits for INT_MIN
11#include <unordered_map>
12using namespace std;
13
14void printSubs(string input, string output)
15{
16 if (input.empty())
17 {
18 cout << output << endl;
19 return;
20 }
21 printSubs(input.substr(1), output);
22 printSubs(input.substr(1), output + input[0]);
23}
24
25void printSubs(string input)
26{
27 printSubs(input, "");
28}
29
30int main()
31{
32 string input;
33 cin >> input;
34 printSubs(input);
35 return 0;
36}