From 357f3ec9cb56a9a3224a268ad1272c23e4588975 Mon Sep 17 00:00:00 2001 From: Masako Toda Date: Sun, 6 Oct 2019 16:51:48 -0700 Subject: [PATCH 1/4] Create lcs.cpp --- LCS/lcs.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 LCS/lcs.cpp diff --git a/LCS/lcs.cpp b/LCS/lcs.cpp new file mode 100644 index 0000000..cebc8f2 --- /dev/null +++ b/LCS/lcs.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +int main() +{ + auto find = [](const std::string& A, const std::string& B) -> int + { + std::vector> LCS(A.length() + 1); + for (unsigned int i = 0; i < LCS.size(); i++) + LCS[i].resize(B.length() + 1); + + for (unsigned int i = 1; i <= A.length(); i++) { + for (unsigned int j = 1; j <= B.length(); j++) { + if (A[i - 1] == B[j - 1]) { + LCS[i][j] = LCS[i - 1][j - 1] + 1; + } + else { + LCS[i][j] = std::max(LCS[i - 1][j], LCS[i][j - 1]) + } + } + } + return LCS[A.length()][B.length()]; + }; + + std::string s1 = "Tsuki ga kirei desu ne"; + std::string s2 = "Taiyo ga mabushii desu ne"; + int len = 0; + if (s1.length() > 0 && s2.length() > 0) + len = find(&s1[0], &s2[0]); + std::cout << len << std::endl; +} From 6e934c6ee3c19a5332ccefe368951ebc1271fbf7 Mon Sep 17 00:00:00 2001 From: Masako Toda Date: Tue, 8 Oct 2019 21:33:45 -0700 Subject: [PATCH 2/4] Create example.cpp --- LCS/example.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 LCS/example.cpp diff --git a/LCS/example.cpp b/LCS/example.cpp new file mode 100644 index 0000000..bced605 --- /dev/null +++ b/LCS/example.cpp @@ -0,0 +1,11 @@ +#include +#include + +extern int lcs(const std::string&, const std::string&); + +int main() +{ + std::string s1 = "Tsuki ga kirei desu ne"; + std::string s2 = "Taiyo ga mabushii desu ne"; + std::cout << lcs(s1, s2) << std::endl; +} From 45f24f90b608ef4ce491cc2c90df3f6461f4c93e Mon Sep 17 00:00:00 2001 From: Masako Toda Date: Tue, 8 Oct 2019 21:35:10 -0700 Subject: [PATCH 3/4] Update lcs.cpp --- LCS/lcs.cpp | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/LCS/lcs.cpp b/LCS/lcs.cpp index cebc8f2..649a2b4 100644 --- a/LCS/lcs.cpp +++ b/LCS/lcs.cpp @@ -1,32 +1,21 @@ -#include #include #include -int main() +int lcs(const std::string& A, const std::string& B) { - auto find = [](const std::string& A, const std::string& B) -> int - { - std::vector> LCS(A.length() + 1); - for (unsigned int i = 0; i < LCS.size(); i++) - LCS[i].resize(B.length() + 1); + std::vector> LCS(A.length() + 1); + for (unsigned int i = 0; i < LCS.size(); i++) + LCS[i].resize(B.length() + 1); - for (unsigned int i = 1; i <= A.length(); i++) { - for (unsigned int j = 1; j <= B.length(); j++) { - if (A[i - 1] == B[j - 1]) { - LCS[i][j] = LCS[i - 1][j - 1] + 1; - } - else { - LCS[i][j] = std::max(LCS[i - 1][j], LCS[i][j - 1]) - } + for (unsigned int i = 1; i <= A.length(); i++) { + for (unsigned int j = 1; j <= B.length(); j++) { + if (A[i - 1] == B[j - 1]) { + LCS[i][j] = LCS[i - 1][j - 1] + 1; + } + else { + LCS[i][j] = std::max(LCS[i - 1][j], LCS[i][j - 1]) } } - return LCS[A.length()][B.length()]; - }; - - std::string s1 = "Tsuki ga kirei desu ne"; - std::string s2 = "Taiyo ga mabushii desu ne"; - int len = 0; - if (s1.length() > 0 && s2.length() > 0) - len = find(&s1[0], &s2[0]); - std::cout << len << std::endl; + } + return LCS[A.length()][B.length()]; } From 82810859fda57ba884689903df7ae2cc1915f57a Mon Sep 17 00:00:00 2001 From: Masako Toda Date: Tue, 8 Oct 2019 21:37:00 -0700 Subject: [PATCH 4/4] Create README.md --- LCS/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 LCS/README.md diff --git a/LCS/README.md b/LCS/README.md new file mode 100644 index 0000000..e96dcfd --- /dev/null +++ b/LCS/README.md @@ -0,0 +1,3 @@ +LCS (Longest Common Subsequence) + +See https://en.wikipedia.org/wiki/Longest_common_subsequence_problem