From b1ea15d245220ffe9a86c6d6c1e1f1f409de30f6 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Tue, 25 Dec 2018 16:56:44 -0600 Subject: Woohoo, longest subsequence (3 seqs) done! --- PlaygroundCpp/Sources/Playground.cpp | 92 ++++++++++++++++++------------------ 1 file 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 &a, vector &b) { +int lcs3(vector &a, vector &b, vector &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 a = { 2, 7, 5 }; -// vector b = { 2, 5 }; -// ASSERT_EQ(2, lcs2(a, b)); -//} -// -//TEST(LCS2, t2) { -// vector 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 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 a = { 1, 2, 3 }; +// vector b = { 2, 1, 3 }; +// vector c = { 1, 3, 5 }; +// ASSERT_EQ(2, lcs3(a, b, c)); //} // -//TEST(LCS2, t3) { -// vector a = { 1, 2, 3 }; -// vector b = { 3, 2, 1 }; -// ASSERT_EQ(1, lcs2(a, b)); +//TEST(LCS3, t2) { +// vector a = { 8, 3, 2, 1, 7 }; +// vector b = { 8, 2, 1, 3, 8, 10, 7 }; +// vector c = { 6, 8, 3, 1, 4, 7 }; +// ASSERT_EQ(3, lcs3(a, b, c)); //} int main() { - size_t n; - std::cin >> n; - vector a(n); - for (size_t i = 0; i < n; i++) { - std::cin >> a[i]; - } - - size_t m; - std::cin >> m; - vector 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 a(an); + for (size_t i = 0; i < an; i++) { + std::cin >> a[i]; + } + size_t bn; + std::cin >> bn; + vector b(bn); + for (size_t i = 0; i < bn; i++) { + std::cin >> b[i]; + } + size_t cn; + std::cin >> cn; + vector c(cn); + for (size_t i = 0; i < cn; i++) { + std::cin >> c[i]; + } + std::cout << lcs3(a, b, c) << std::endl; } + -- cgit v1.2.3