cpp: add «1143. Longest Common Subsequence»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
da19846d3f
commit
421b783a60
1 changed files with 45 additions and 0 deletions
45
cpp/longest-common-subsequence.cpp
Normal file
45
cpp/longest-common-subsequence.cpp
Normal file
|
@ -0,0 +1,45 @@
|
|||
#include <cassert>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
auto dp_helper(const std::string &t1, const std::string &t2,
|
||||
std::vector<int> &dp, int i, int j) -> int {
|
||||
if (i < 0 || j < 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto idx = i * t2.size() + j;
|
||||
if (dp[idx] != -1) {
|
||||
return dp[idx];
|
||||
}
|
||||
|
||||
if (t1[i] == t2[j]) {
|
||||
dp[idx] = 1 + dp_helper(t1, t2, dp, i - 1, j - 1);
|
||||
} else {
|
||||
dp[idx] = std::max(dp_helper(t1, t2, dp, i - 1, j),
|
||||
dp_helper(t1, t2, dp, i, j - 1));
|
||||
}
|
||||
|
||||
return dp[idx];
|
||||
}
|
||||
|
||||
public:
|
||||
auto longestCommonSubsequence(const std::string &text1,
|
||||
const std::string &text2) -> int {
|
||||
int m = text1.size();
|
||||
int n = text2.size();
|
||||
std::vector<int> dp(m * n, -1);
|
||||
return dp_helper(text1, text2, dp, m - 1, n - 1);
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
Solution s;
|
||||
|
||||
assert(s.longestCommonSubsequence("abcde", "ace") == 3);
|
||||
assert(s.longestCommonSubsequence("abc", "abc") == 3);
|
||||
assert(s.longestCommonSubsequence("abc", "def") == 0);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue