P1344 最长回文长度
思路
区间动归
#include <bits/stdc++.h>
using namespace std;
const int maxn=1006;
int a[maxn][maxn];
int main(int argc, char const *argv[]) {
char s[maxn];
while (cin>>s) {
int len=strlen(s);
for(int i=0;i<len;i++)
for(int j=0;j<len;j++)
a[i][j]=0;
for(int i=0;i<len;i++){
a[i][i]=1;
if(s[i+1]==s[i]) {a[i][i+1]=2;}
else a[i][i+1]=1;
}
for(int k=3;k<=len;k++){
for(int i=0;i<len;i++){
int j=i+k-1;
if(j>=len) continue;
if(s[i]==s[j]){
a[i][j]=a[i][j]<a[i+1][j-1]+2?a[i+1][j-1]+2:a[i][j];
}else {
a[i][j]=a[i+1][j]<a[i][j-1]?a[i][j-1]:a[i+1][j];
}
}
}
std::cout << a[0][len-1] << '\n';
}
return 0;
}