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.

输入样例

1
1
4
1 1 2 1
6
2 1 1 2 1 3
0

输出样例

1
4
5


本题关键在理解题意,求最长环形回文串,枚举断点两端最长回文串长度之和即为答案
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int a[N];
int dp[N][N];
int n;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
while(cin>>n,n)
{
int maxn=-1;
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;++i)
{cin>>a[i];dp[i][i]=1;}
for(int len=2;len<=n;++len)//枚举区间长度
for(int i=1;i<=n;++i)
{
int j=i+len-1;//终点
if(j>n) break;
dp[i][j]=max(dp[i+1][j],dp[i][j-1]);
if(a[i]==a[j])
dp[i][j]=max(dp[i][j],dp[i+1][j-1]+2);
}
for(int i=1;i<=n;++i)
{
maxn=max(maxn,dp[1][i]+dp[i+1][n]);
}
cout<<maxn<<'\n';
}
return 0;
}

hdu:Two Rabbits(区间DP)的更多相关文章

  1. hdu 4745 Two Rabbits 区间DP

    http://acm.hdu.edu.cn/showproblem.php?pid=4745 题意: 有两只兔子Tom Jerry, 他们在一个用石头围城的环形的路上跳, Tom只能顺时针跳,Jerr ...

  2. hdu 5396 Expression(区间dp)

    Problem Description Teacher Mai has n numbers a1,a2,⋯,anand n−1 operators("+", "-&quo ...

  3. You Are the One HDU - 4283 (区间DP)

    Problem Description The TV shows such as You Are the One has been very popular. In order to meet the ...

  4. Dire Wolf HDU - 5115(区间dp)

    Dire Wolf Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)Total ...

  5. HDU 5568 sequence2 区间dp+大数

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5568 题意: 求所有长度为k的严格升序子序列的个数. 题解: 令dp[i][k]表示以i结尾的长度为 ...

  6. HDU4745 - Two Rabbits(区间DP)

    题目大意 给出一个长度为n的环状序列,两只兔子各自从一个点出发,一个顺时针跳,一个逆时针跳,每个时刻都要求两只兔子所在的数字是相同的,兔子最多跳一个圈~~~问兔子们最多能跳多少次 题解 一个逆时针跳, ...

  7. hdu 4579 博弈+区间dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597 #include <cstdio> #include <cstring> ...

  8. Hdu 2513 区间DP

    Cake slicing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. HDU 4745 Two Rabbits(区间DP,最长非连续回文子串)

    Two Rabbits Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total ...

  10. HDU 4283---You Are the One(区间DP)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=4283 Problem Description The TV shows such as Y ...

随机推荐

  1. 生成虚拟mac地址通过dhcp获取ip,耗尽dhco地址池

    平台:kali 软件:dhcpstarv 命令 dhcpstarv  -i 网卡名称 -e 本机IP 查看dhcp服务器已经分发的地址 cat /tmp/dhcp.leases

  2. Yolov3-v5正负样本匹配机制

    ​ 本文来自公众号"AI大道理". ​ 什么是正负样本? 正负样本是在训练过程中计算损失用的,而在预测过程和验证过程是没有这个概念的. 正样本并不是手动标注的GT. 正负样本都是针 ...

  3. (转载)Overlay网络

    1. Overlay 网络1.1 Overlay 技术概述    Overlay 在网络技术领域,指的是一种网络架构上叠加的虚拟化技术模式,其大体框架是对基础网络不进行大规模修改的条件下,实现应用在网 ...

  4. STP协议-基础

    生成树协议 一 .技术背景一个缺乏冗余性设计的网络:任何一个网络节点出现故障,会造成单链路故障.单设备故障,使整个网络瘫痪. 引入冗余性的同时也引入了二层环路:网络的冗余性增强了,但是却出现了二层环路 ...

  5. Vulnhub 靶场 LOOZ: 1

    Vulnhub 靶场 LOOZ: 1 前期准备: 靶机地址:https://www.vulnhub.com/entry/looz-1,732/ kali攻击机ip:192.168.147.190 靶机 ...

  6. HTML学习笔记3----制作一个简易网站

    随笔记录方便自己和同路人查阅. #------------------------------------------------我是可耻的分割线--------------------------- ...

  7. 面试题-react

    对react的理解 是什么 React 是一个用于构建用户界面的 JavaScript 库. 能干什么 可以通过组件化的方式构建大型的,快速响应的大型web应用 如何做 声明式 React 使用jsx ...

  8. MySQL_demo_50道习题

    title: MySQL_demo_50道习题 author: 杨晓东 permalink: MySQL_demo date: 2021-10-02 11:27:04 categories: - 投篮 ...

  9. ABAP 动态内表 实例展示以及代码Demo

    因根据查询条件展示的报表列数不一定一致,因此采用动态内表的方式进行处理 这里写了一个简单的Demo记录一下 效果如下图: 一般来说通过选择屏幕来控制列的,我这里就简单处理,直接对内表赋予相关值,包括相 ...

  10. vue项目中如何使用markdown编辑器插件

    1.安装mavon-editor $ npm install mavon-editor --save 需要使用Markdown编辑器的页面js中: import { mavonEditor } fro ...