问题描述:https://leetcode-cn.com/problems/delete-operation-for-two-strings/description/
考察最长公共序列
class Solution {
public:
static int lcs(string str1, string str2)
{
int len1 = str1.length();
int len2 = str2.length();
int **c=new int *[len1+1];
for(int i=0;i<len1+1;i++)
c[i]= new int[len2+1];//为每行分配空间(每行中有col个元素)
for (int i = 0; i <= len1; i++) {
for( int j = 0; j <= len2; j++) {
if(i == 0 || j == 0) {
c[i][j] = 0;
} else if (str1[i-1] == str2[j-1]) {
c[i][j] = c[i-1][j-1] + 1;
} else {
c[i][j] = max(c[i - 1][j], c[i][j - 1]);
}
}
}
return c[len1][len2];
}
int minDistance(string word1, string word2) {
return word1.length()+ word2.length()-2*lcs(word1,word2);
}
};
最长公共子序列与最长公共子串(DP)
https://blog.youkuaiyun.com/u012102306/article/details/53184446#
本文探讨了LeetCode上的一道题——两字符串的删除操作,并深入讲解了如何利用动态规划求解最长公共子序列问题。通过具体代码实现,展示了如何计算两个字符串的最长公共子序列长度,进而解决题目要求的最小删除次数问题。
355

被折叠的 条评论
为什么被折叠?



