forked from akshay2742/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCS_Rishi.cpp
More file actions
67 lines (57 loc) · 1.47 KB
/
LCS_Rishi.cpp
File metadata and controls
67 lines (57 loc) · 1.47 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
/*
author: Rishi Mohan Jha
created_at: 22-12-2021 10:25 PM
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(a) (a).begin(),(a).end()
string a, b;
void LCS(int n, int m) {
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
if (i == 0 || j == 0)
dp[i][j] = 0;
else if (a[i - 1] == b[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];
string ans;
int cnt = 1;
int i = n, j = m;
while (i > 0 && j > 0) {
if (a[i - 1] == b[j - 1]) {
ans += b[j - 1];
i--;
j--;
}
else {
if (dp[i][j - 1] > dp[i - 1][j]) {
j--;
}
else {
i--;
}
}
}
reverse(all(ans));
cout << ans << endl;
}
void Solve() {
cin >> a >> b;
int n = sz(a), m = sz(b);
LCS(n, m);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); int t = 1;
while (t--) Solve();
}