题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla
算法2
(动态规划) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
int len1 = text1.size();
int len2 = text2.size();
if(len1 == 0 || len2 == 0)
return 0;
vector<vector<int>> dp(len2 + 1, vector<int>(len1 + 1));
int maxVal = 0;
for(int i = 1; i <= len2; ++i)
{
for(int j = 1; j <= len1; ++j)
{
if(text2[i - 1] == text1[j - 1])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
maxVal = max(maxVal, dp[i][j]);
}
}
return maxVal;
}
};