问题描述: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#