文章
37
粉丝
1
获赞
353
访问
24.6k
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
int mode;
cin>>s>>mode;
int n = s.size();
vector<vector<string>> dp(n,vector<string>(n,""));
//dp[i][j]:(i,j)的最长回文串
for(int i = 0 ; i < n; i++){
dp[i][i] += s[i];
}
for(int i = n-1; i >= 0; i--){
for(int j = i+1; j < n; j++){
if(s[i] == s[j]){
dp[i][j] = s[i]+dp[i+1][j-1]+s[j];
}
else{
if(dp[i+1][j].size() > dp[i][j-1].size()){
dp[i][j] = dp[i+1][j];
}
else if(dp[i][j-1].size() > dp[i+1][j].size()){
dp[i][j] = dp[i][j-1];
}
else{
dp[i][j] = min(dp[i+1][j],dp[i][j-1]);
}
}
}
}
if(mode==1){
cout<<dp[0][n-1].size()<<endl;
}
else if(mode==2){
cout<<dp[0][n-1]<<endl;
}
}
登录后发布评论
暂无评论,来抢沙发