-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllConstruct.cpp
More file actions
75 lines (75 loc) · 2.27 KB
/
AllConstruct.cpp
File metadata and controls
75 lines (75 loc) · 2.27 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
#include<iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
// Time Complexity is O(n^m) And Space Complexity is O(n^m).
vector<vector<string>> allConstruct(string *ar,int n,const string& str,map<string,vector<vector<string>>> &mp){
if (str.empty()){
return vector<vector<string>>(1,vector<string>());
}
if (mp.count(str)!=0){
return mp[str];
}
vector<vector<string>> res = vector<vector<string>>();
for (int i = 0; i < n; ++i) {
if (str.substr(0,ar[i].length())==ar[i]){
vector<vector<string>> v = allConstruct(ar,n,str.substr(ar[i].length()),mp);
for (auto & j : v) {
j.push_back(ar[i]);
res.push_back(j);
}
}
}
mp[str] = res;
return mp[str];
}
// Time Complexity is O(n^m) And Space Complexity is O(n^m).
vector<vector<string>> allConstruct2(string *ar,int n,const string& str){
vector<vector<string>> dp[str.length()+1];
dp[0] = vector<vector<string>>(1,vector<string>());
for (int i = 1; i <=str.length() ; ++i) {
for (int j = 0; j < n; ++j) {
if (i>=ar[j].length() && str.substr(i-ar[j].length(),ar[j].length())==ar[j] && !dp[i-ar[j].length()].empty()){
for (int k = 0; k < dp[i-ar[j].length()].size(); ++k) {
dp[i].push_back(dp[i-ar[j].length()][k]);
dp[i][dp[i].size()-1].push_back(ar[j]);
}
}
}
}
return dp[str.length()];
}
int main(){
cout<<"Enter The value of n:\n";
int n;
cin>>n;
cout<<"Enter The value of array:\n";
auto *ar = new string [n];
for (int i = 0; i < n; ++i) {
cin>>ar[i];
}
cout<<"Enter The Target String:\n";
string str;
cin>>str;
map<string ,vector<vector<string>>> mp;
cout<<"Using Top Down Approach\n";
vector<vector<string>> v = allConstruct(ar,n,str,mp);
for (auto & i : v) {
reverse(i.begin(),i.end());
}
for (auto & i : v) {
for (auto & j : i) {
cout<<j<<" ";
}
cout<<"\n";
}
cout<<"Using Bottom Up Approach\n";
v = allConstruct2(ar,n,str);
for (auto & i : v) {
for (auto & j : i) {
cout<<j<<" ";
}
cout<<"\n";
}
}