HDU 4745 Two Rabbits (2013杭州网络赛1008,最长回文子串)
2013-09-17 23:58
441 查看
Two Rabbits
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 505 Accepted Submission(s): 260
[align=left]Problem Description[/align]
Long long ago, there lived two rabbits Tom and Jerry in the forest. On a sunny afternoon, they planned to play a game with some stones. There were n stones on the ground and they were arranged as a clockwise ring. That is to say, the first stone was adjacent to the second stone and the n-th stone, and the second stone is adjacent to the first stone and the third stone, and so on. The weight of the i-th stone is ai.
The rabbits jumped from one stone to another. Tom always jumped clockwise, and Jerry always jumped anticlockwise.
At the beginning, the rabbits both choose a stone and stand on it. Then at each turn, Tom should choose a stone which have not been stepped by itself and then jumped to it, and Jerry should do the same thing as Tom, but the jumping direction is anti-clockwise.
For some unknown reason, at any time , the weight of the two stones on which the two rabbits stood should be equal. Besides, any rabbit couldn't jump over a stone which have been stepped by itself. In other words, if the Tom had stood on the second stone, it cannot jump from the first stone to the third stone or from the n-the stone to the 4-th stone.
Please note that during the whole process, it was OK for the two rabbits to stand on a same stone at the same time.
Now they want to find out the maximum turns they can play if they follow the optimal strategy.
[align=left]Input[/align]
The input contains at most 20 test cases.
For each test cases, the first line contains a integer n denoting the number of stones.
The next line contains n integers separated by space, and the i-th integer ai denotes the weight of the i-th stone.(1 <= n <= 1000, 1 <= ai <= 1000)
The input ends with n = 0.
[align=left]Output[/align]
For each test case, print a integer denoting the maximum turns.
[align=left]Sample Input[/align]
1
1
4
1 1 2 1
6
2 1 1 2 1 3
0
[align=left]Sample Output[/align]
1
4
5
Hint
For the second case, the path of the Tom is 1, 2, 3, 4, and the path of Jerry is 1, 4, 3, 2.
For the third case, the path of Tom is 1,2,3,4,5 and the path of Jerry is 4,3,2,1,5.
[align=left]Source[/align]
2013 ACM/ICPC Asia Regional Hangzhou Online
[align=left]Recommend[/align]
liuyiding
答案竟然就是分成两部分以后的最长回文子串,
太难想到了,TAT
/* *********************************************** Author :kuangbin Created Time :2013/9/15 星期日 15:20:03 File Name :2013杭州网络赛\1008.cpp ************************************************ */ #pragma comment(linker, "/STACK:1024000000,1024000000") #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; #define REP(I, N) for (int I=0;I<int(N);++I) #define FOR(I, A, B) for (int I=int(A);I<int(B);++I) #define DWN(I, B, A) for (int I=int(B-1);I>=int(A);--I) #define REP_1(I, N) for (int I=1;I<=int(N);++I) #define FOR_1(I, A, B) for (int I=int(A);I<=int(B);++I) #define DWN_1(I, B, A) for (int I=int(B);I>=int(A);--I) #define REP_C(I, N) for (int N____=int(N),I=0;I<N____;++I) #define FOR_C(I, A, B) for (int B____=int(B),I=A;I<B____;++I) #define DWN_C(I, B, A) for (int A____=int(A),I=B-1;I>=A____;--I) #define REP_1_C(I, N) for (int N____=int(N),I=1;I<=N____;++I) #define FOR_1_C(I, A, B) for (int B____=int(B),I=A;I<=B____;++I) #define DWN_1_C(I, B, A) for (int A____=int(A),I=B;I>=A____;--I) #define DO(N) while(N--) #define DO_C(N) int N____ = N; while(N____--) #define TO(i, a, b) int s_=a<b?1:-1,b_=b+s_;for(int i=a;i!=b_;i+=s_) #define TO_1(i, a, b) int s_=a<b?1:-1,b_=b;for(int i=a;i!=b_;i+=s_) #define SQZ(I, J, A, B) for (int I=int(A),J=int(B)-1;I<J;++I,--J) #define SQZ_1(I, J, A, B) for (int I=int(A),J=int(B);I<=J;++I,--J) const int MAXN = 1010; int a[MAXN]; int dp[MAXN][MAXN]; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n; while(scanf("%d",&n) ==1 && n) { for(int i = 1;i <= n;i++) scanf("%d",&a[i]); memset(dp,0,sizeof(dp)); for(int i = 1;i <= n;i++)dp[i][i] = 1; for(int k = 1;k <= n;k++) for(int i = 1;i + k <= n;i++) { dp[i][i+k] = max(dp[i+1][i+k],dp[i][i+k-1]); if(a[i] == a[i+k])dp[i][i+k] = max(dp[i][i+k],2+dp[i+1][i+k-1]); } int ans = 0; for(int i = 1;i <= n;i++) ans = max(ans,dp[1][i]+dp[i+1] ); printf("%d\n",ans); } return 0; }
相关文章推荐
- HDU 4745 Two Rabbits(区间DP,最长非连续回文子串)
- HDU 4745 Two Rabbits(区间DP,最长非连续回文子串)
- HDU 4745 Two Rabbits (最长回文变形)
- HDU 4745 Two Rabbits(最长回文子序列)(2013 ACM/ICPC Asia Regional Hangzhou Online)
- HDU 4777 Rabbit Kingdom (2013杭州赛区1008题,预处理,树状数组)
- hdu 4745 Two Rabbits(dp最长回文子序列)
- hdu 3068 最长回文子串 Manacher算法
- hdu 3068 最长回文 【Manacher求最长回文子串,模板题】
- HDU 3068 最长回文(求最长回文子串)
- HDU 4747 Mex (2013杭州网络赛1010题,线段树)
- HDU 3068 最长回文(求最长回文子串)
- hdu 4741 Save Labman No.004(2013杭州网络赛)
- hdu【最长回文子串——Manacher算法】POJ 3974/HDU 3068
- 2013杭州网络赛D题HDU 4741(计算几何 解三元一次方程组)
- hdu 4745 Two Rabbits 2013 ACM/ICPC Asia Regional Hangzhou Online
- hdu 4745 区间回文最长长度
- HDU 3068 [最长回文子串]
- hdu 4738 2013杭州赛区网络赛 桥+重边+连通判断 ***
- HDU 3068 最长回文子串(manacher算法)
- hdu 3068 最长回文(manacher&最长回文子串)