diff options
author | Haidong Ji | 2018-12-25 16:56:44 -0600 |
---|---|---|
committer | Haidong Ji | 2018-12-25 16:56:44 -0600 |
commit | b1ea15d245220ffe9a86c6d6c1e1f1f409de30f6 (patch) | |
tree | 0f14e151379571fefcedcf4d0dcab15611f1c641 | |
parent | 1559f265e9f1fcbedd1510949d02f7e661e652a7 (diff) |
Woohoo, longest subsequence (3 seqs) done!
-rw-r--r-- | PlaygroundCpp/Sources/Playground.cpp | 92 |
1 files changed, 47 insertions, 45 deletions
diff --git a/PlaygroundCpp/Sources/Playground.cpp b/PlaygroundCpp/Sources/Playground.cpp index 8c8b854..e402792 100644 --- a/PlaygroundCpp/Sources/Playground.cpp +++ b/PlaygroundCpp/Sources/Playground.cpp @@ -5,61 +5,63 @@ using std::vector; -int lcs2(vector<int> &a, vector<int> &b) { +int lcs3(vector<int> &a, vector<int> &b, vector<int> &c) { + int l = c.size(); int m = b.size(); int n = a.size(); - int D[n + 1][m + 1]; - for (int j = 0; j < m + 1; j++) { - for (int i = 0; i < n + 1; i++) { - // Initialize row/column 0 to 0 is necessary! - if (i == 0 || j == 0) { - D[i][j] = 0; - continue; - } - if (a.at(i - 1) == b.at(j - 1)) { - D[i][j] = D[i - 1][j - 1] + 1; - } else { - D[i][j] = std::max(D[i - 1][j], D[i][j - 1]); + int D[n + 1][m + 1][l + 1]; + for (int k = 0; k < l + 1; k++) { + for (int j = 0; j < m + 1; j++) { + for (int i = 0; i < n + 1; i++) { + if (i == 0 || j == 0 || k == 0) { + D[i][j][k] = 0; + continue; + } + if (a[i - 1] == b[j - 1] && b[j - 1] == c[k - 1]) { + D[i][j][k] = D[i - 1][j - 1][k - 1] + 1; + } else { + D[i][j][k] = std::max(D[i - 1][j][k], + std::max(D[i][j - 1][k], D[i][j][k - 1])); + } } } } - return D[n][m]; + return D[n][m][l]; } -//TEST(LCS2, t1) { -// vector<int> a = { 2, 7, 5 }; -// vector<int> b = { 2, 5 }; -// ASSERT_EQ(2, lcs2(a, b)); -//} -// -//TEST(LCS2, t2) { -// vector<int> a = { 1, 9, 2, 5, 5, 5, 8, 3, 5, 11, 0, 2, 4, 1, 7, 4, 5, 9, 2, -// 4, 0, 3, 1, 5, 8, 3, 5, 9, 7, 8, 4, 5, 6, 7 }; -// vector<int> b = { 6, 3, 8, 3, 1, 8, 3, 5, 0, 9, 6, 8, 3, 5, 7, 9, 1, 4, 7, -// 9, 3, 5, 9, 0, 1, 4, 2, 7, 5, 8, 9, 3, 5, 6, 1, 6, 3, 1, 6, 7, 3 }; -// ASSERT_EQ(17, lcs2(a, b)); +//TEST(LCS3, t1) { +// vector<int> a = { 1, 2, 3 }; +// vector<int> b = { 2, 1, 3 }; +// vector<int> c = { 1, 3, 5 }; +// ASSERT_EQ(2, lcs3(a, b, c)); //} // -//TEST(LCS2, t3) { -// vector<int> a = { 1, 2, 3 }; -// vector<int> b = { 3, 2, 1 }; -// ASSERT_EQ(1, lcs2(a, b)); +//TEST(LCS3, t2) { +// vector<int> a = { 8, 3, 2, 1, 7 }; +// vector<int> b = { 8, 2, 1, 3, 8, 10, 7 }; +// vector<int> c = { 6, 8, 3, 1, 4, 7 }; +// ASSERT_EQ(3, lcs3(a, b, c)); //} int main() { - size_t n; - std::cin >> n; - vector<int> a(n); - for (size_t i = 0; i < n; i++) { - std::cin >> a[i]; - } - - size_t m; - std::cin >> m; - vector<int> b(m); - for (size_t i = 0; i < m; i++) { - std::cin >> b[i]; - } - - std::cout << lcs2(a, b) << std::endl; + size_t an; + std::cin >> an; + vector<int> a(an); + for (size_t i = 0; i < an; i++) { + std::cin >> a[i]; + } + size_t bn; + std::cin >> bn; + vector<int> b(bn); + for (size_t i = 0; i < bn; i++) { + std::cin >> b[i]; + } + size_t cn; + std::cin >> cn; + vector<int> c(cn); + for (size_t i = 0; i < cn; i++) { + std::cin >> c[i]; + } + std::cout << lcs3(a, b, c) << std::endl; } + |