-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
33 lines (30 loc) · 830 Bytes
/
Copy pathsolution.cpp
File metadata and controls
33 lines (30 loc) · 830 Bytes
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
class Solution
{
public:
int minSuperSeq(string &s1, string &s2)
{
int n = s1.size(), m = s2.size();
// Ensure s2 is the shorter one to use O(min(n,m)) memory
if (m > n)
return minSuperSeq(s2, s1);
vector<int> prev(m + 1, 0), cur(m + 1, 0);
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
if (s1[i - 1] == s2[j - 1])
{
cur[j] = 1 + prev[j - 1];
}
else
{
cur[j] = max(prev[j], cur[j - 1]);
}
}
prev.swap(cur);
fill(cur.begin(), cur.end(), 0);
}
int lcs = prev[m];
return n + m - lcs;
}
};