Description
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.
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.
Input
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.
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.
Output
For each test case, print a integer denoting the maximum turns.
Sample Input
1 1 4 1 1 2 1 6 2 1 1 2 1 3 0
Sample Output
1 4 5
居然求环的回文串还可以这么玩~~
本题题意是:一个环,两只兔子一只顺时针走,一只逆时针走,从头一个起点开始,每步两只都需要选择相同的数,最多走一圈,问最多走几步?
开始以为是求最多的点数==然后遇到环就想把环倍增,然而依旧不会。题解说是求出1-n的dp[i][j]值为区间内的回文串长度,然后把串分成两半,两边求和取最大值即可。为什么呢?将两侧子串的回文中点都可以当做开始的点,就是这里,我又读错题了,我以为起点是同一个石头。。。纠结了半天,用ac代码读入回文串的长度是偶数的情况,结果和我想的不一样才又看的==
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int num[2009],dp[1009][1009],n;
int main()
{
while(~scanf("%d",&n)&&n)
{
for(int i=1;i<=n;i++)scanf("%d",&num[i]);
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++) dp[i][i]=1;
for(int len=1;len<=n;len++)
{
for(int l=1;l+len<=n;l++)
{
int r=l+len;
if(num[l]==num[r])
dp[l][r]=dp[l+1][r-1]+2;
else dp[l][r]=max(dp[l+1][r],dp[l][r-1]);
}
}
int ans=1;
for(int i=1;i<n;i++)
if(ans<dp[1][i]+dp[i+1][n])
ans=dp[1][i]+dp[i+1][n];
printf("%d\n",ans);
}
return 0;
}