Two Rabbits_dp求最长不连续回文子序列

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.

题意:

两只兔子,玩一个游戏,有n块石头围成圈,每块石头有不同的权值,两只兔子一个顺时针,一个逆时针跳跃,要求每次兔子调到的石头权值相同,并且不能回到或超过一开始的石头,求最多能经过的石头数。

【思路】dp求最长不连续回文子序列。分别求1到i和i+1到n的最长回文序列。

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;

const int N=1010;
int n;
int dp[N][N],a[N];

int main()
{
    while(scanf("%d",&n)!=EOF,n)
    {
        memset(dp, 0, sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            dp[i][i]=1;
        }
        for(int i=n;i>=1;i--)
        {
            for(int j=i+1;j<=n;j++)
            {
                if(a[i]==a[j])
                    dp[i][j]=dp[i+1][j-1]+2;
                else dp[i][j]=max(max( dp[i+1][j],dp[i][j-1]), dp[i+1][j-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];
        }
        cout<<ans<<endl;

    }
    return 0;
}
时间: 2024-10-02 11:31:00

Two Rabbits_dp求最长不连续回文子序列的相关文章

[LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数

Given a string S, find the number of different non-empty palindromic subsequences in S, and return that number modulo 10^9 + 7. A subsequence of a string S is obtained by deleting 0 or more characters from S. A sequence is palindromic if it is equal

Manacher(最长递减回文串)

http://acm.hdu.edu.cn/showproblem.php?pid=4513 Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一个新的队形,新的队形若满足以下三点要求,则就是新的完美队形: 1.挑出的人保持原队形的相对顺序不变,且必须都是在原队形中连续的: 2.左右对称,假设有m个人形成新的队形,则第1个人和第m个人身高相同,第2

516 Longest Palindromic Subsequence 最长回文子序列

给定一个字符串s,找到其中最长的回文子序列.可以假设s的最大长度为1000. 详见:https://leetcode.com/problems/longest-palindromic-subsequence/description/ C++: class Solution { public: int longestPalindromeSubseq(string s) { int n = s.size(); vector<vector<int>> dp(n, vector<in

hdu4632 Palindrome subsequence 回文子序列个数 区间dp

Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/Others)Total Submission(s): 4513    Accepted Submission(s): 1935 Problem Description In mathematics, a subsequence is a sequence that can be derived f

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

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

UVa 11404 回文子序列(LCS求最长回文串长度)

https://vjudge.net/problem/UVA-11404 题意: 给定一个由小写字母组成的字符串,删除其中的0个或多个字符,使得剩下的字母(顺序不变)组成一个尽量长的回文串.如果有多解,输出字典序最小的解. 思路: 首先,最长回文子串的长度可以通过正序字符串和逆序字符串进行LCS得出. 但是这道题目麻烦的是还要输出这个回文串,并且字典序得最小. 应用的主要还是LCS的思想方法,不过在进行状态转移的时候,再加上字符串的状态转移. 不过最后得到的字符串不一定是回文串,但是它的前一半肯

soj 4421 最长回文子序列

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

bzoj 2565: 最长双回文串

Description 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为"abc",逆序为"cba",不相同).输入长度为n的串S,求S的最长双回文子串T,即可将T分为两部分X,Y,(|X|,|Y|≥1)且X和Y都是回文串. Input 一行由小写英文字母组成的字符串S. Output 一行一个整数,表示最长双回文子串的长度. Sample Input baacaabbacabb Sample Output 12 HINT

BZOJ2565:最长双回文串

2565: 最长双回文串 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2195  Solved: 1119[Submit][Status][Discuss] Description 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为"abc",逆序为"cba",不相同). 输入长度为n的串S,求S的最长双回文子串T,即可将T分为两部分X,Y,(|X|,|Y|≥1)且X和Y都