给出一个序列,求回文串的最大长度。
基本思路思路很简单,直接套用晴神算法笔记的模板就可以,不够好像有更优的算法(马拉车算法),这里没有给出,我的代码所有测试点也都通过了。需要注意的是,在状态转移方程这一步,为了防止状态值dp为空,需要枚举子串长度。
代码#include#include #include #include using namespace std; const int maxn=1010; int ans=1;//最大回文子串的长度 string a; int dp[maxn][maxn];//dp[i][j]表示序列a[i]~a[j]是否是回文序列。初始化为0 int main(){ getline(cin,a); int len=a.length(); //初始化数组dp memset(dp,0,sizeof(dp)); //给递推边界赋值 for(int i=0;i



