解题思路:
(1)动态规划
class Solution {
public:
/**
* longest common substring
* @param str1 string字符串 the string
* @param str2 string字符串 the string
* @return string字符串
*/
string LCS(string str1, string str2) {
int m=str1.length(),n=str2.length();
vector<vector<int>> v(m+1,vector<int>(n+1,0));
int maxs=0,end=0;
for(int i=1;i<m+1;i++) {
for(int j=1;j<n+1;j++) {
if(str1[i-1]==str2[j-1]) v[i][j]=v[i-1][j-1]+1;
if(maxs<v[i][j]) {
maxs=v[i][j];
end=j;
}
}
}
return str2.substr(end-maxs,maxs);
}
};