HDU 4745-Rabbits "discontinuous longest palindrome subsequence, interval DP"

Source: Internet
Author: User

SubmitStatus

Description

Long ago, there lived and 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's to say, the first stone were adjacent to the second stone and the n-th stone, and the second stone are adjacent to T He 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 has 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, in any time, the weight of the and the both stones on which the rabbits stood should is equal. Besides, any rabbit couldn ' t jump over a stone which has been stepped by itself. In and words, if the Tom had stood on the second stone, it cannot jump from the first stone to the third stone or from t He n-the Stone to the 4-th stone.

Please note that during the whole process, it is OK for the both rabbits to stand in 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 the most 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 <=, 1 <= ai <= 1000)
The input ends with n = 0.

Output

For each test case, print a integer denoting the maximum turns.

Sample Input

Sample Output


Actually seek ring of palindrome string can also play so ~ ~

The subject Test instructions is: a ring, two rabbits a clockwise walk, a counterclockwise walk, starting from the beginning of a starting point, each step two need to choose the same number, up to walk a lap, ask the most walk a few steps?

Began to think is the most points = = and then encountered the ring to multiply the ring, but still will not. The 1-n is to find out the dp[i][j] value of the palindrome string length within the interval, and then divide the string into two halves, both sides sum to take the maximum value. Why is it? The two sides of the sub-string of palindrome can be used as the starting point, that is, here, I read the wrong question, I thought the starting point is the same stone ... Tangled half a day, with the AC code read into the palindrome string length is even, the result and I think not the same only to see the = =

#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;}


HDU 4745-Rabbits "discontinuous longest palindrome subsequence, interval dp"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.