-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec10_80.cpp
More file actions
125 lines (96 loc) Β· 3.15 KB
/
Copy pathlec10_80.cpp
File metadata and controls
125 lines (96 loc) Β· 3.15 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
vector<string> all_longest_common_subsequences(string s, string t) {
// Code here
int n = s.size(), m = t.size();
vector<vector<int>> dp(n+1, vector<int>(m+1, 0)); // DP table initialized to 0
// Fill the DP table
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s[i-1] == t[j-1])
dp[i][j] = 1 + dp[i-1][j-1];
else
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
int len = dp[n][m]; // Answer is in the last cell
vector<string> ans(len , '$')
int index = len -1;
int i = n , j = m;
while(i>0 && j>0){
if(s[i-1] == t[j-1]){
ans[index] = s[i-1];
index--;
i-- , j--;
}
else if(dp[i-1][j] >dp[i][j-1]){
i--;
}
else j--;
}
return ans;
}
};
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
vector<string> all_longest_common_subsequences(string s1, string s2) {
int n1 = s1.size(), n2 = s2.size();
// Step 1: Create and Fill DP Table in Reverse Order
vector<vector<int>> dp(n1 + 1, vector<int>(n2 + 1, 0));
for (int i = n1 - 1; i >= 0; i--) {
for (int j = n2 - 1; j >= 0; j--) {
if (s1[i] == s2[j])
dp[i][j] = 1 + dp[i + 1][j + 1];
else
dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]);
}
}
// Step 2: Backtracking with Memoization
unordered_set<string> ans; // Using unordered_set for efficiency
unordered_set<string> visited; // To avoid duplicate recursive calls
function<void(int, int, string)> f = [&](int i, int j, string lcs) {
if (i == n1 || j == n2) {
ans.insert(lcs); // Store the LCS found
return;
}
string key = to_string(i) + "," + to_string(j) + "," + lcs;
if (visited.count(key)) return; // Skip if already visited
visited.insert(key);
if (s1[i] == s2[j]) {
f(i + 1, j + 1, lcs + s1[i]);
} else {
if (dp[i][j] == dp[i][j + 1]) f(i, j + 1, lcs);
if (dp[i][j] == dp[i + 1][j]) f(i + 1, j, lcs);
}
};
f(0, 0, "");
vector<string> result(ans.begin(), ans.end());
sort(result.begin(), result.end()); // Sort once at the end
return result;
}
};
//{ Driver Code Starts.
int main() {
int T;
cin >> T;
while (T--) {
string s, t;
cin >> s >> t;
Solution ob;
vector<string> ans = ob.all_longest_common_subsequences(s, t);
for (auto i : ans)
cout << i << " ";
cout << "\n";
cout << "~" << endl;
}
return 0;
}
// } Driver Code Ends