文章
34
粉丝
89
获赞
2
访问
18.5k
#include<bits/stdc++.h>
using namespace std;
int dp[105][105];
int main(){
string s1,s2;
cin >> s1 >> s2;
int len1 = s1.size();
int len2 = s2.size();
memset(dp,0,sizeof(dp));
int ans = 0;
int end = 0;
for(int i = 1;i <= len1;i++){
for(int j = 1;j <= len2;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]);
dp[i][j] = 0;
}
if(ans < dp[i][j]){
ans = dp[i][j];
end = j - 1;
}
}
}
// cout << dp[6][6];
string str = "";
for(int i = end,k =0;k < ans;k++){
str += s2[i - k];
}
reverse(str.begin(),str.end());
cout << str << endl;
}
登录后发布评论
暂无评论,来抢沙发