HDU 4745 Two Rabbits(最长回文子序列)(2013 ACM/ICPC Asia Regional Hangzhou Online)

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.
 
题目大意:有一个环n个点,每个点有一个权值,两只兔子各自从任意点出发,一只顺时针跳,一只逆时针条,任意时刻两只兔子只能落在权值相同的点上,两只兔子都只能跳一个圈,问最多能跳多少下。
思路:用dp[l][r]表示,一只兔子从 l 开始顺时针跳,另一只兔子从 r 开始逆时针跳,直到兔子 r 跳到 l,兔子 l 跳到 r,最多能跳多少步。
然后,可以发现出发点必然是相同或者相邻(最优解肯定会相遇一次,相遇的时候只可能是a[i]和a[i+1]相同,互相跳过,或者落在同一个点,我们可以把那个最优解的起点挪到这里)
那么ans = max(dp[i-1][i+1]+1, dp[i][i+1]), i ∈ [0, n - 1]
 
代码(125MS):
 1 #include <cstdio>

 2 #include <cstring>

 3 #include <algorithm>

 4 using namespace std;

 5 

 6 const int MAXN = 1010;

 7 

 8 int dp[MAXN][MAXN], a[MAXN];

 9 int pre[MAXN], next[MAXN];

10 int n;

11 

12 int dfs(int l, int r) {

13     if(dp[l][r]) return dp[l][r];

14     if(pre[l] == r) return dp[l][r] = 1 + (a[l] == a[r]);

15     if(a[l] == a[r]) dp[l][r] = dfs(pre[l], next[r]) + 2;

16     else dp[l][r] = max(dfs(pre[l], r), dfs(l, next[r]));

17     return dp[l][r];

18 }

19 

20 int main() {

21     while(scanf("%d", &n) != EOF && n) {

22         for(int i = 0; i < n; ++i) scanf("%d", &a[i]);

23         if(n < 3) printf("%d\n", n);

24         else {

25             memset(dp, 0, sizeof(dp));

26             for(int i = 0; i < n; ++i)

27                 pre[i] = (i - 1 + n) % n, next[i] = (i + 1) % n, dp[i][i] = 1;

28             int ans = 0;

29             for(int i = 0; i < n; ++i)

30                 ans = max(ans, max(dfs(pre[i], i), dfs(pre[i], next[i]) + 1));

31             printf("%d\n", ans);

32         }

33     }

34 }
View Code

 

你可能感兴趣的:(online)