-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlongest-common-substring.cpp
70 lines (58 loc) · 1.46 KB
/
longest-common-substring.cpp
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
/**
* @file longest-common-substring.cpp
* @author prakash ([email protected])
* @brief
The longest common substring (not subsequence) of two strings X and Y is
the longest string that appears as a run of consecutive letters in both strings.
For example, the longest common substring of photograph and tomography is
ograph.
(a) Let n = |X| and m = |Y |. Give a Θ(nm) dynamic programming algorithm for
longest common substring based on the longest common subsequence/edit distance
algorithm.
(b) Give a simpler Θ(nm) algorithm that does not rely on dynamic
programming.
* @version 0.1
* @date 2021-08-26
*
* @copyright Copyright (c) 2021
*
*/
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
void LCS(string X, string Y) {
int n = X.size(), m = Y.size();
int dp[n + 1][m + 1];
memset(dp, 0, sizeof(dp));
int max_;
int row, col;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (X[i - 1] == Y[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
}
if (dp[i][j] > max_) {
max_ = dp[i][j];
row = i;
col = j;
}
}
}
cout << " length : " << max_ << endl;
vector<char> res(n+1);
while (dp[row][col] != 0) {
res.push_back(X[row - 1]);
row--;
col--;
}
for (auto x : res) {
cout << x;
}
}
int main(int argc, const char **argv) {
string X = "photograph", Y = "tomography";
LCS(X, Y);
return 0;
}