当前位置: 代码迷 >> 综合 >> HDU 4745 Two Rabbits(非连续最长回文子序列,区间DP)
  详细解决方案

HDU 4745 Two Rabbits(非连续最长回文子序列,区间DP)

热度:96   发布时间:2024-01-15 06:48:30.0

Two Rabbits(最长回文子串的长度)

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2398    Accepted Submission(s): 1244

 

Problem 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.

 

 

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.

 

 

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

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.

 

 

 

Source

2013 ACM/ICPC Asia Regional Hangzhou Online

 

 

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  6408 6407 6406 6405 6404 

题意:

给定一个环状数列,求一个最长的回文子序列

分析:

环状数列很好解决,直接把数组扩开一倍即可。

我们定义f[l][r]表示l到r区间的最长非连续的的回文子序列总体长度

如果s[l]==s[r] 则 f[l][r]=f[l+1][r-1]+2;

不然 f[l][r]=max(f[l+1][r],f[l][r-1]);

要点

阶段:区间长度

状态:开始端点

决策:两种情况

状态转移方程:

s[l]==s[r] 则 f[l][r]=f[l+1][r-1]+2;

不然 f[l][r]=max(f[l+1][r],f[l][r-1]);

初始条件:f[i][i]=1,其余为0

目的:长度为n的所有dp[i][j]

但上述仅仅是求出一个序列的非连续最长回文子序列,题目的序列是环状的,先上正确答案代码

for (int i = 1; i <= n; i++){//cout<<dp[1][i]<<" "<<dp[i+1][n]<<endl;ans = max(ans, dp[1][i] + dp[i + 1][n]);}

我们想一下因为他是一个环,1~i之间分两份,i~n之间分两分

则b+c和a+d构成一个更大的回文串

代码实现:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<vector>using namespace std;inline int read()
{int x=0,f=1;char ch=getchar();while (!isdigit(ch)) {if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;
}const int maxn = 3010;int a[maxn],f[maxn][maxn];
int n,m;
int ans;int main()
{ while (1){scanf("%d",&n);memset(f,0,sizeof(f));memset(a,0,sizeof(a));if (n==0) return 0;ans=0;      for (int i=1;i<=n;i++)scanf("%d",&a[i]),a[i+n]=a[i];for (int i=1;i<=2*n;i++)f[i][i]=1;for (int i=2;i<=2*n;i++){for (int l=1;l<=2*n-i+1;l++){int r=l+i-1;if (a[l]==a[r]) f[l][r]=max(f[l][r],f[l+1][r-1]+2);else f[l][r]=max(f[l][r-1],f[l+1][r]);}}for (int i=1;i<=n;i++){ans=max(ans,f[1][i]+f[i+1][n]);}printf("%d\n",ans);}return 0;
}