-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest_common_subsequence.cpp
74 lines (61 loc) · 1.24 KB
/
longest_common_subsequence.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
// Author: Jakub Pawlina
// Algorithm: LCS (Longest Common Subsequence)
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
std::string LCS(std::string first, std::string second)
{
int lcs_length[first.length() + 1][second.length() + 1];
for (int i = 0; i <= first.length(); ++i)
{
for (int j = 0; j <= second.length(); ++j)
{
if (!i || !j)
{
lcs_length[i][j] = 0;
}
else if (first[i - 1] == second[j - 1])
{
lcs_length[i][j] = lcs_length[i - 1][j - 1] + 1;
}
else
{
lcs_length[i][j] = std::max(lcs_length[i - 1][j], lcs_length[i][j - 1]);
}
}
}
int index = lcs_length[first.length()][second.length()];
std::string lcs = "";
int i = first.length();
int j = second.length();
while (i > 0 && j > 0)
{
if (first[i - 1] == second[j - 1])
{
lcs = first[i - 1] + lcs;
i--; j--;
}
else if (lcs_length[i - 1][j] > lcs_length[i][j - 1])
{
i--;
}
else
{
j--;
}
}
return lcs;
}
int32_t main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
std::string first;
std::cin >> first;
std::string second;
std::cin >> second;
std::string result = LCS(first, second);
std::cout << result.length() << '\n';
std::cout << result << '\n';
return 0;
}