题解 | 最长公共子串
最长公共子串
https://www.nowcoder.com/practice/f33f5adc55f444baa0e0ca87ad8a6aac?tpId=295&tqId=991150&sourceUrl=%2Fexam%2Foj%3FquestionJobId%3D10%26subTabName%3Donline_coding_page
class Solution {
public:
string LCS(string str1, string str2) {
int m = str1.length();
int n = str2.length();
// dp[i][j] 表示以str1[i-1]和str2[j-1]结尾的最长公共子串长度
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
int maxLen = 0; // 记录最长公共子串的长度
int endPos = 0; // 记录最长公共子串在str1中的结束位置
// 填充dp表
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (str1[i - 1] == str2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
// 更新最长公共子串信息
if (dp[i][j] > maxLen) {
maxLen = dp[i][j];
endPos = i - 1; // 记录在str1中的结束位置
}
} else {
dp[i][j] = 0;
}
}
}
// 根据结束位置和长度提取子串
if (maxLen == 0) {
return "";
}
return str1.substr(endPos - maxLen + 1, maxLen);
}
};
使用动态规划来解决: 定义 dp[i][j] 表示以 str1[i-1] 和 str2[j-1] 结尾的最长公共子串长度 状态转移方程: 如果 str1[i-1] == str2[j-1],则 dp[i][j] = dp[i-1][j-1] + 1 否则 dp[i][j] = 0 在填充dp表的过程中记录最长公共子串的长度和结束位置
动态规划 空间换时间 文章被收录于专栏
动态规划通常用于求解最优解问题, 动态规划算法与分治法类似,其基本思想也是将待求解问题分解成若干子问题,先求解子问题, 然后从这些子问题的解得到原问题的解。与分治法不同的是,适合于用动态规划求解的问题, 经分解得到的子问题往往不是互相独立的。

