标签:二维 int 子序列 ext 规划 转移 tips ext2 max
dp数组的定义:
二维dp数组,dp[i] [j]代表在text1[0, ... , i-1]和text2[0, ... , j-1]时,两个字符串的最长公共子序列的长度。
base_case:
dp[0] [j] = dp[i] [0] =0
状态转移方程:
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
int m = text1.size() + 1;
int n = text2.size() + 1;
int a[m][n];
for(int i=0; i<m ; i++){
a[i][0]=0;
}
for(int j=1; j<n ; j++){
a[0][j] = 0;
}
for(int i=1; i<m ; i++){
for(int j=1; j<n ; j++){
if(text1[i-1] == text2[j-1]){
a[i][j] = a[i-1][j-1] + 1;
}
else{
a[i][j] = max(a[i-1][j],a[i][j-1]);
}
}
}
return a[m-1][n-1];
}
};
标签:二维 int 子序列 ext 规划 转移 tips ext2 max
原文地址:https://www.cnblogs.com/lzyrookie/p/14727122.html