-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortestCommonSupersequence.cpp
More file actions
34 lines (33 loc) · 1.05 KB
/
shortestCommonSupersequence.cpp
File metadata and controls
34 lines (33 loc) · 1.05 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
class Solution {
public:
string shortestCommonSupersequence(string str1, string str2) {
int l1 = str1.size(), l2 = str2.size();
vector<vector<int>> dp(l1 + 1, vector<int>(l2 + 1, 0));
for (int i = 1; i <= l1; ++i) {
for (int j = 1; j <= l2; ++j) {
if (str1[i - 1] == str2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
}
else dp[i][j] = std::max(dp[i][j - 1], dp[i - 1][j]);
}
}
string ans;
ans.reserve(l1 + l2 - dp[l1][l2]);
while (l1 || l2) {
if (l1 == 0)
ans += str2[--l2];
else if (l2 == 0)
ans += str1[--l1];
else {
if (dp[l1][l2] == dp[l1 - 1][l2])
ans += str1[--l1];
else if (dp[l1][l2] == dp[l1][l2 - 1])
ans += str2[--l2];
else
ans += str1[--l1], --l2;
}
}
reverse(ans.begin(), ans.end());
return ans;
}
};