-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaditya_verma_recursion_12_13.cpp
More file actions
107 lines (72 loc) · 2.69 KB
/
aditya_verma_recursion_12_13.cpp
File metadata and controls
107 lines (72 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<bits/stdc++.h>
using namespace std;
//===================generating all substrings=================================
/*
i/p o/p
abc _
bc _ bc a
c _ c b c a c ab
_ _ _ c _ b _ bc _ a _ ac _ ab _ abc ---->o/p on leaves bcz input is empty here....
*/
void generate_substring(string input,string output){
if(input.length()==0){
cout<<output<<" ";
return;
}
string out_dont_choose=output;
string out_choose=output;
out_choose.push_back(input.at(0));
input.erase(input.begin()+0);
generate_substring(input,out_dont_choose);
generate_substring(input,out_choose);
return;
}
//======================generating unique substring===========================
/*
i/p o/p
abc _
bc _ bc a
c _ c b c a c ab
_ _ _ c _ b _ bc _ a _ ac _ ab _ abc ---->will store the o/p strings in leaves in a map and then output it altogrther in order to remove repeated substrings.
*/
void print_substr(unordered_set<string>& sub_str){
//reverse(sub_str.begin(),sub_str.end());
for(auto itr=sub_str.begin();itr!=sub_str.end();itr++){
cout<<*itr<<" ";
}
}
void generate_unique_substring(unordered_set<string>& sub_str,string input,string output){
if(input.length()==0){
sub_str.insert(output);
return;
}
string out_dont_choose=output;
string out_choose=output;
out_choose.push_back(input.at(0));
input.erase(input.begin()+0);
generate_unique_substring(sub_str,input,out_dont_choose);
generate_unique_substring(sub_str,input,out_choose);
return;
}
int main() {
//user input
string inp;
string out;
cin>>inp;
out="";
//generate all substrings
generate_substring(inp,out);
cout<<"\n";
//generate all unique substrings
unordered_set<string> sub_str; //if the order in which substring is obtained doesnt matter then we can even take normal set(which will sort the substrings while insertion)
generate_unique_substring(sub_str,inp,out);
print_substr(sub_str); //print the unique subset
return 0;
}
/*
i/p o/p
abc _
bc _ bc a
c _ c b c a c ab
_ _ _ c _ b _ bc _ a _ ac _ ab _ abc ---->o/p on leaves bcz input is empty here....
*/