class Solution {
public:
/**
* longest common substring
* @param str1 string字符串 the string
* @param str2 string字符串 the string
* @return string字符串
*/
string LCS(string str1, string str2) {
// write code here
int l1 = str1.size();
int l2 = str2.size();
vector<vector<int>>dp(l1 + 1,vector<int>(l2 + 1,0));
int max_len = 0;
int max_last_index = 0;
for(int i = 0; i < l1; i++){
for(int j = 0; j < l2; j++){
if(str1[i] == str2[j]){
dp[i+1][j+1] = dp[i][j] + 1;
if(max_len < dp[i+1][j+1]){
max_len = dp[i+1][j+1];//记录最长的公共子串
max_last_index = i;//记录最长公共子串的结束位置
}
}else{
dp[i+1][j+1] = 0;
}
}
}
return str1.substr(max_last_index - max_len + 1, max_len);//最后再对字符串进行截取即可
}
};