-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathF_LCS.cpp
More file actions
83 lines (74 loc) · 1.83 KB
/
Copy pathF_LCS.cpp
File metadata and controls
83 lines (74 loc) · 1.83 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
/** بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم
* Author : Sakib62
* Created : Tue__05-Mar-2024__14:27:10
* File : F_LCS.cpp
**/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s, t;
cin >> s >> t;
int sLen = s.length();
int tLen = t.length();
int dp[sLen][tLen];
for (int i = 0; i < sLen; i++) {
for (int j = 0; j < tLen; j++) {
int cnt = (s[i] == t[j]);
if (!i && !j) {
dp[i][j] = cnt;
}
else if (!i) {
dp[i][j] = max(dp[i][j-1], cnt);
}
else if (!j) {
dp[i][j] = max(dp[i-1][j], cnt);
}
else {
if (cnt) {
dp[i][j] = 1 + dp[i-1][j-1];
}
else {
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
}
}
// for (int i = 0; i < sLen; i++) {
// for (int j = 0; j < tLen; j++) {
// cout << dp[i][j] << " ";
// }
// cout << "\n";
// }
int row = sLen - 1, col = tLen - 1;
string ans;
while (row >= 0 && col >= 0) {
if (s[row] == t[col]) {
ans += s[row];
row--;
col--;
continue;
}
if (row == 0) {
col--;
continue;
}
if (col == 0) {
row--;
continue;
}
if (dp[row-1][col] > dp[row][col-1]) {
row--;
}
else {
col--;
}
}
//cout << dp[sLen-1][tLen-1] << "\n";
reverse(ans.begin(), ans.end());
cout << ans << "\n";
}