From 334aa22a28ba55d33e8978048b342ea0e5481d56 Mon Sep 17 00:00:00 2001 From: yenru0 Date: Mon, 7 Oct 2024 09:58:22 +0900 Subject: [PATCH] complete 5582.py 9251.cpp --- zeta_cpp/completed/9251.cpp | 48 +++++++++++++++++++++++++++++++++++ zeta_python/completed/5582.py | 18 +++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 zeta_cpp/completed/9251.cpp create mode 100644 zeta_python/completed/5582.py diff --git a/zeta_cpp/completed/9251.cpp b/zeta_cpp/completed/9251.cpp new file mode 100644 index 0000000..4061ba6 --- /dev/null +++ b/zeta_cpp/completed/9251.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include + +using namespace std; + +int lenLCSeq(string s1, string s2) { + vector> DP(s1.length(), vector(s2.length(), 0)); + + for (int i = 0; i < s1.length(); i++) { + for (int j = 0; j < s2.length(); j++) { + if (s1[i] == s2[j]) { + if (i == 0 && j == 0) { + DP[i][j] = 1; + } else if (i == 0 && j != 0) { + DP[i][j] = 1; + } else if (i != 0 && j == 0) { + DP[i][j] = 1; + } else { + DP[i][j] = DP[i - 1][j - 1] + 1; + } + } else { + if (i == 0 && j == 0) { + + } else if (i == 0 && j != 0) { + DP[i][j] = DP[i][j - 1]; + } else if (i != 0 && j == 0) { + DP[i][j] = DP[i - 1][j]; + } else { + DP[i][j] = max(DP[i - 1][j], DP[i][j - 1]); + } + } + } + } + + return DP[s1.length() - 1][s2.length() - 1]; +} + +int main(void) { + string s1, s2; + + cin >> s1 >> s2; + + cout << lenLCSeq(s1, s2) << endl; + + return 0; +} \ No newline at end of file diff --git a/zeta_python/completed/5582.py b/zeta_python/completed/5582.py new file mode 100644 index 0000000..4fa232e --- /dev/null +++ b/zeta_python/completed/5582.py @@ -0,0 +1,18 @@ +def LCS(s1, s2) -> int: + MAXSP = (0, 0, 0) + DP = [[0 for _ in range(len(s2))] for _ in range(len(s1))] + for i, c1 in enumerate(s1): + for j, c2 in enumerate(s2): + if c1 == c2: + if i >= 1 and j >= 1: + DP[i][j] = DP[i - 1][j - 1] + 1 + else: + DP[i][j] = 1 + if MAXSP[0] < DP[i][j]: + MAXSP = (DP[i][j], i, j) + return MAXSP[0] + + +if __name__ == "__main__": + s1, s2 = input(), input() + print(LCS(s1, s2))