http://acm.hdu.edu.cn/showproblem.php?pid=4745

题意:

有两只兔子Tom Jerry, 他们在一个用石头围城的环形的路上跳, Tom只能顺时针跳,Jerry只能逆时针跳,  要求在跳的过程中他们所在石头的权值必须相同,而且只能单向跳,中间不能有已经跳过的石头。

思路:

模型就是求环上的最长回文串,我们只要将原串倍增,然后每个长度为n的子串的最长回文串就是我们要求的。区间DP一下就好了, 注意要考虑起点终点是统一点的情况特殊。

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue> #define CL(arr, val) memset(arr, val, sizeof(arr)) #define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0)
#define ll long long
#define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define keyTree (chd[chd[root][1]][0])
#define Read() freopen("din.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout); #define M 107
#define N 1007 using namespace std; int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1}; const int inf = 0x7f7f7f7f;
const int mod = 1000000007;
const double eps = 1e-8;
const int R = 100007; int dp[2*N][2*N];
int a[2*N];
int n; int main()
{
while (~scanf("%d",&n))
{
if (!n) break;
CL(dp,0);
for (int i = 1; i <= n; ++i)
{
scanf("%d",&a[i]);
a[i + n] = a[i];
dp[i][i] = dp[i + n][i + n] = 1;
}
//没句串的长度
for (int i = 2; i <= n; ++i)
{
for (int j = 1; j + i - 1 <= 2*n; ++j)
{
int k = j + i - 1;
if (a[j] == a[k]) dp[j][k] = dp[j + 1][k - 1] + 2;
else
{
dp[j][k] = max(dp[j][k], max(dp[j + 1][k], dp[j][k - 1]));
}
}
}
int ans = 0;
for (int i = 1; i <= n; ++i) ans = max(ans, dp[i][i + n - 1]);
//起点终点是同一点的情况
for (int i = 1; i <= n; ++i) ans = max(ans, dp[i][i + n - 2] + 1);
printf("%d\n",ans);
}
return 0;
}

  

hdu 4745 Two Rabbits 区间DP的更多相关文章

  1. HDU 4745 Two Rabbits 区间dp_回文序列

    题目链接: http://blog.csdn.net/scnu_jiechao/article/details/11759333 Two Rabbits Time Limit: 10000/5000 ...

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

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

  3. HDU 5115 Dire Wolf 区间dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5115 Dire Wolf Time Limit: 5000/5000 MS (Java/Others ...

  4. HDU 5693 D Game 区间dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5693 题解: 一种朴实的想法是枚举选择可以删除的两个或三个数(其他的大于三的数都能凑成2和3的和), ...

  5. hdu 4597 Play Game 区间dp

    Play Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=459 ...

  6. hdu 5693 && LightOj 1422 区间DP

    hdu 5693 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5693 等差数列当划分细了后只用比较2个或者3个数就可以了,因为大于3的数都可以由2和3 ...

  7. hdu 5181 numbers——思路+区间DP

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5181 题解:https://www.cnblogs.com/Miracevin/p/10960717.ht ...

  8. HDU 1141---Brackets Sequence(区间DP)

    题目链接 http://poj.org/problem?id=1141 Description Let us define a regular brackets sequence in the fol ...

  9. HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)

    题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...

随机推荐

  1. 豆瓣API接口开发,结合angularJS来做,感觉爽歪歪!

    第一次做还是先说下API 是什么鬼? API : application program interface 应用程序编程接口: 有那些常见的API: webAPI : 通过WEB方式提供结构叫 WE ...

  2. oracle缩写与术语

    术语 定义活动会话池 资源组或子计划允许的当前活动会话数ADDM Automatic Database Diagnostic Monitor(自动数据库诊断监视程序)ASM Automatic Sto ...

  3. [EF]vs15+ef6+mysql这个问题,你遇到过么?

    写在前面 因为最近用mysql比较多,所以想了解下ef+mysql的内容,发现ef连接mysql数据库,还有那么一段路折腾.折腾到最后,发疯了. 步骤 这里采用db first的方式来使用ef. 通过 ...

  4. MVC认识

    1.ASP.NET两种开发模式的简单比较(WebForm和MVC) (1)WebForm开发模式 当用户输入网址https://i.cnblogs.com/EditPosts.aspx?opt=1进行 ...

  5. SpringIoc的精彩讲解

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  6. UVA796 - Critical Links(Tarjan求桥)

    In a computer network a link L, which interconnects two servers, is considered critical if there are ...

  7. sql server 2008维护计划配置

    转自:https://blog.csdn.net/yunye114105/article/details/6594826 一:目标任务: 使用SQL Server 2008自带的维护计划自动备份数据库 ...

  8. mysql 数据操作 单表查询 group by 分组 目录

    mysql 数据操作 单表查询 group by 介绍 mysql 数据操作 单表查询 group by 聚合函数 mysql 数据操作 单表查询 group by 聚合函数 没有group by情况 ...

  9. sshd:root@notty解决方法

    sshd:root@notty解决方法 [复制链接]--http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=2050551 cat /e ...

  10. sipp保持sip协议的tcp长连接

    sipp这个工具非常有用,但是由于应用手册不完善,加上资料非常少,所以要用熟练是非常不容易的.