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

SubmitStatus

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 

居然求环的回文串还可以这么玩~~

本题题意是:一个环,两只兔子一只顺时针走,一只逆时针走,从头一个起点开始,每步两只都需要选择相同的数,最多走一圈,问最多走几步?

开始以为是求最多的点数==然后遇到环就想把环倍增,然而依旧不会。题解说是求出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;
}
时间: 2024-08-29 12:34:39

HDU 4745 Two Rabbits【非连续最长回文子序列,区间DP】的相关文章

最长回文子序列 区间dp

J - 买票回家啦 Time Limit:1000MS    Memory Limit:65535KB    64bit IO Format: SubmitStatusPracticeNBUT 1586 Description 集训要结束了,同学们就准备回家了.好舍不得回家阿.(那就再待一个月嘛,就这么愉快地决定了.)超哥要回家了,可是他没有挤进12306官网, 可怜的他就随便找了个能代购车票的网站.结果,当他付钱时傻眼了,这个网站竟然要验证码.验证码嘛就照着样子输入就好了呀,哦不,这个网站管理

最长回文子序列(不连续)以及最长回文子串(连续)

整理了一下关于回文子序列和回文子串的程序. 其中(1)和(2)是采用动态规划的思想写出的回文子序列的程序,这种子序列就是在原始的串中可以不连续,比如对于那种要求删除几个字符来得到最长的回文字符串的题就是这种情况. 比如caberbaf.  最长的子序列是5 abeba 或者abrba.而子串最长只有1 (3)(4)(5)都是最长子串的求法.(3)是暴力求解,(4)是改进的暴力求解.(5)采用的是动态规划的方法. 1 #include <iostream> 2 #include <stri

hdu4757 最长回文子序列(区间DP)

http://acm.hdu.edu.cn/showproblem.php?pid=4745 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

soj 4421 最长回文子序列

题意: 给你一个字符串,求该字符串的最长回文子序列长度. 解法: 以前做过连续最长回文子串的长度就是通过构造奇数偶数长度的来做,而本题是不连续. 注意到回文字符串的特点是从左边向右边看和从右边向左边看是一样的效果,那么就可以把目标字符串s导致后产生一个t,子串中如果t和s相同那么这个子串就是回文子串,那么就转化为这两个子串求LCS(longest common subsequent)的问题了. 我的代码: #include <set> #include <map> #include

最长回文子序列

题目:给你一个字符串,求它的最长回文子序列,比如"bbbab" 最长回文子序列是"bbbb" 所以返回4,,"abab"最长子序列是"aba"或者"bab" 所以返回3 思路:和之前做的几道dp不同,,,也是我不够变通,,打dp表的时候总习惯左上到右下的顺序,但是这个顺序却固化了我的思维,忽略了对于题解本身含义的理解....... 这个题从下到上开始打表,最重要的是它的含义,,,知道dp[i][j]意味着什

[最长回文子序列Manacher]4.11终于出线了啊!

Manacher用来求最长回文子序列 1.读入字符串,在每个字符前后加一个没有在原字符串中出现的字符,这样不论是奇数或者偶数个都变成了奇数个 例如: M A N A C H E R # M # A # N # A # C # H # E # R # 2.在开头和末尾再添加一个没有在原字符串中出现的字符,这样就不用讨论越界了 例如: # M # A # N # A # C # H # E # R # $# M # A # N # A # C # H # E # R # \0 3.然后从第一位开始计

算法导论_动态规划_最长回文子序列

一.问题的描述 回文序列(Palindromic sequence, Palindrome)是指正向遍历和反向遍历完全相同的序列,例如字符串"AAAAA"显然是一个回文序列,又如字符串"[email protected]"也是一个回文序列.现在,我们要在一个(字符)序列中找出最长回文子序列的长度.例如字符序列"BBABCBCAB",最长回文子序列是"BACBCAB"(可能不唯一),它的长度是7:子序列"BBBBB&q

HDU 4745 Two Rabbits(最长回文子序列)

http://acm.hdu.edu.cn/showproblem.php?pid=4745 题意: 有一个环,现在有两只兔子各从一个点开始起跳,一个沿顺时针,另一个沿逆时针,只能在一圈之内跳,并且每次所在的点的大小必须相同,问最多能经过 几个点. 思路:环状的话可以先倍增改成链. 这道题目的话就是求最长回文子串,它的求法是这样的: 设字符串为str,长度为n,p[i][j]表示第i到第j个字符间的子序列的个数(i<=j),则: 状态初始条件:dp[i][i]=1 (i=0:n-1) 状态转移方

hdu 4745 Two Rabbits 最长回文子序列

 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 f