-
Notifications
You must be signed in to change notification settings - Fork 14
/
longest_common_substring.cpp
87 lines (69 loc) · 1.65 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <algorithm>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
template <class type_t>
type_t longest_common_substring(const type_t& A, const type_t& B)
{
//best[i][j] = int length of longest common substring
// between A[0,i] and B[0,j]
vector<vector<int>> best(A.size()+1, vector<int>(B.size()+1));
//recursion:
for (int i=1; i<A.size(); ++i)
{
for (int j=1; j<B.size(); ++j)
{
if (A[i-1] == B[j-1])
{
best[i][j] = best[i-1][j-1] + 1;
}
else
{
best[i][j] = max(best[i-1][j], best[i][j-1]);
}
}
}
type_t result;
//backtrace
int i = A.size(), j = B.size();
while (i > 0 && j > 0)
{
if (A[i-1] == B[j-1])
{
result.push_back(A[i-1]);
--i; --j;
}
else if (best[i-1][j] > best[i][j-1])
--i;
else
--j;
}
reverse(result.begin(), result.end());
return result;
}
int main()
{
ifstream fin("./data/lcs.in");
ofstream fout("./data/lcs.out");
int N, M;
fin >> N >> M;
vector<int> A(N);
for (int i = 0; i < N; ++i)
fin >> A[i];
vector<int> B(M);
for (int i = 0; i < M; ++i)
fin >> B[i];
vector<int> result;
result = longest_common_substring(A, B);
fout << result.size() << "\n";
cout << result.size() << "\n";
for (auto it = result.begin(); it < result.end(); ++it)
{
fout << *it << " ";
cout << *it << " ";
}
fout << "\n";
cout << "\n";
return 0;
}