好像是leetcode上的题,不知道洛谷有没有。
给定一个字符串s,找到其中最长的回文子序列,并返回该序列的长度。
输入样例1:
bbbab
输出样例1:
4
输入样例2:
cbbd
输出样例2:
2
样例都过了,但是AC3个WA2个
#include<iostream>
#include<string>
using namespace std;
string s;
int f[1005][1005];
int main(){
cin>>s;
s=" "+s;
int len=s.length()-1;
for(int i=len;i>=1;i--){
for(int j=i+1;j<=len;j++){
if(s[i]==s[j]){
f[i][j]=f[i+1][j-1]+2;
}else{
f[i][j]=max(f[i+1][j],f[i][j-1]);
}
}
}
cout<<f[1][len];
return 0;
}