hdu 4745 Two Rabbits 区间DP
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的更多相关文章
- HDU 4745 Two Rabbits 区间dp_回文序列
题目链接: http://blog.csdn.net/scnu_jiechao/article/details/11759333 Two Rabbits Time Limit: 10000/5000 ...
- HDU 4745 Two Rabbits(区间DP,最长非连续回文子串)
Two Rabbits Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total ...
- HDU 5115 Dire Wolf 区间dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5115 Dire Wolf Time Limit: 5000/5000 MS (Java/Others ...
- HDU 5693 D Game 区间dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5693 题解: 一种朴实的想法是枚举选择可以删除的两个或三个数(其他的大于三的数都能凑成2和3的和), ...
- hdu 4597 Play Game 区间dp
Play Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=459 ...
- hdu 5693 && LightOj 1422 区间DP
hdu 5693 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5693 等差数列当划分细了后只用比较2个或者3个数就可以了,因为大于3的数都可以由2和3 ...
- hdu 5181 numbers——思路+区间DP
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5181 题解:https://www.cnblogs.com/Miracevin/p/10960717.ht ...
- HDU 1141---Brackets Sequence(区间DP)
题目链接 http://poj.org/problem?id=1141 Description Let us define a regular brackets sequence in the fol ...
- HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)
题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...
随机推荐
- 【BZOJ4774/4006】修路/[JLOI2015]管道连接 斯坦纳树
[BZOJ4774]修路 Description 村子间的小路年久失修,为了保障村子之间的往来,法珞决定带领大家修路.对于边带权的无向图 G = (V, E),请选择一些边,使得1 <= i & ...
- xcode7/ios9中 低版本app运行时,屏幕上下出现黑边的问题
xcode从低版本升级至 7.0或更高版本后,某些低版本app再次编译运行后,发现app在设备上运行时,会在上端和底部 出现黑边的现象.这导致app的展示界面跟缩水了一样,变得十分丑陋. 对于这一问题 ...
- iOS面试3
转:http://studentdeng.github.io/blog/2014/02/11/baidu-interview/ 百度面试 FEB 11TH, 2014 | COMMENTS 百度移动云 ...
- dubbo有什么作用
转自:http://blog.csdn.net/ichsonx/article/details/39008519 1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的R ...
- c++Template 的辨析
1.在c++Template中很多地方都用到了typename与class这两个关键字,而且好像可以替换,是不是这两个关键字完全一样呢? 答:class用于定义类,在模板引入c++后,最初定义模板的方 ...
- .NET中将中文符号转换成英文符号
public static string ConvertToEn(string text) { const string s1 = ".:,?!.“”‘’"; const stri ...
- Intellij IDEA常用配置详解
1. IDEA内存优化 先看看你机器本身的配置而配置. \IntelliJ IDEA 8\bin\idea.exe.vmoptions -------------------------------- ...
- centos 搭建php环境
安装Apache1.安装yum -y install httpd2.开启apache服务systemctl start httpd.service3.设置apache服务开机启动systemctl e ...
- ASP.netMVC文件下载的几种方法
第一种:最简单的超链接方法,标签的href直接指向目标文件地址,这样容易暴露地址造成盗链,这里就不说了 第二种:后台下载 在后台下载中又可以细分为几种下载方式 首先,在前台,我们需要一个标签 &quo ...
- Python多进程编程(转)
原文:http://www.cnblogs.com/kaituorensheng/p/4445418.html 阅读目录 1. Process 2. Lock 3. Semaphore 4. Even ...