标签:dp
#include<stdio.h> #include<string.h> #include<iostream> using namespace std; int dp[2010][2010]; int max(int a,int b) { return a>b?a:b; } int main() { int n,i,j; int num[2100]; while(~scanf("%d",&n),n) { for(i=1;i<=n;i++) { scanf("%d",&num[i]); num[i+n]=num[i]; } memset(dp,0,sizeof(dp)); int Max=0; for(i=n;i>=1;i--) { dp[i][i]=1; for(j=i+1;j<=n;j++) { if(num[i]==num[j]) dp[i][j]=dp[i+1][j-1]+2; else dp[i][j]=max(dp[i+1][j],dp[i][j-1]); } } for(i=1;i<=n;i++) if(dp[1][i]+dp[i+1][n]>Max) Max=dp[1][i]+dp[i+1][n]; printf("%d\n",Max); } return 0; }
标签:dp
原文地址:http://blog.csdn.net/zxf654073270/article/details/45458597