题解 | #最长公共子串#
最长公共子串
https://www.nowcoder.com/practice/f33f5adc55f444baa0e0ca87ad8a6aac
class Solution { public: int maxLen = 0; int indexI; string LCS(string str1, string str2) { vector<vector<int>> dp(str1.length() + 1, vector<int>(str2.length() + 1, 0)); for (int i = 1; i <= str1.length(); i++) { for (int j = 1; j <= str2.length(); 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]; indexI = i - 1; } } } string s = ""; for (int i = maxLen; i > 0; i--) { s = str1[indexI--] + s; } return s; } };