hdu 4745 Two Rabbits

思路:求最长回文子串的长度!
代码如下:

 

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<algorithm>
 4 #include<iomanip>
 5 #include<cmath>
 6 #include<cstring>
 7 using namespace std;
 8 int s[3001],dp[3001][3001];
 9 int dfs(int p,int q)
10 {
11       if(dp[p][q]!=-1) return dp[p][q];
12       if(p>q) return dp[p][q]=0;
13       dp[p][q]=max(dfs(p+1,q),dfs(p,q-1));
14       if(s[p]==s[q]) dp[p][q]=max(dp[p][q],dfs(p+1,q-1)+1);
15       return dp[p][q];
16 }
17 int main()
18 {
19     int n;
20     while(scanf("%d",&n)&&n){
21             for(int i=1;i<=n;i++){
22                   scanf("%d",&s[i]);
23                   s[2*n+i]=s[n+i]=s[i];
24             }
25             int ans=0;
26             memset(dp,-1,sizeof(dp));
27             for(int i=1;i<=n;i++)
28                   ans=max(ans,dfs(i,2*n+i-1));
29             printf("%d\n",ans);
30     }
31     return 0;
32 }
View Code

 

 

 

posted @ 2013-09-16 12:16  _随心所欲_  阅读(248)  评论(0编辑  收藏  举报