最长公共子串(LCS,Longest Common Substring)
思考:
s1 = ‘abcdefg’
s2 = ‘defabcd’
class Solution {
public:
int longestCommonSubstring(string s1, string s2) {
if(s1.empty() || s2.empty())
return 0;
int m = s1.size(), n = s2.size();
vector<vector<int>> dp(m+1, vector<int>(n+1, 0));
int res=0;
for(int i = 1; i <= m ; i++){
for(int j = 1; j <= n; j++) {
if(s1[i] == s2[j]) {
dp[i][j] = dp[i-1][j-1]+1;
res = max(res, dp[i][j]);
}
}
}
return res;
}
};