#include <algorithm>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* longest common subsequence
* @param s1 string字符串 the string
* @param s2 string字符串 the string
* @return string字符串
*/
string LCS(string s1, string s2) {
// write code here
string a="";
int n=s1.size(),m=s2.size();
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(s1[i-1]==s2[j-1])
{
dp[i][j]=dp[i-1][j-1]+1;
}
else{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
}
int i=n,j=m;
while(i>0&&j>0)
{
if(s1[i-1]==s2[j-1])
{
a+=s1[i-1];
j--;
i--;
}
else{
if(dp[i-1][j]>dp[i][j-1])
{
i--;
}
else{
j--;
}
}
}
if(a.empty())return "-1";
reverse(a.begin(),a.end());
return a;
}
};