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(),l2 = str2.size();
int rec[l2];
memset(rec,0,sizeof(rec));
vector<int> ret{0,0};
auto up = [=,&ret](int a,int b){
ret[0] = a;
ret[1] = b;
};
for(int i=0;i<l1;i++){
for(int j=l2;j>=0;j--){
if(str1[i] == str2[j]){
rec[j] = rec[j-1] + 1;
}else{
rec[j] = 0;
}
//rec[i][j] = max(rec[i][j],rec[i-1][j]);
//rec[i][j] = max(rec[i][j],rec[i][j-1]);
if(rec[j] > ret[1])
up(i,rec[j]);
}
}
return str1.substr(ret[0]-ret[1]+1, ret[1]);
}
};