Manacher HDOJ 5371 Hotaru's problem
/*
题意:求形如(2 3 4) (4 3 2) (2 3 4)的最长长度,即两个重叠一半的回文串
Manacher:比赛看到这题还以为套个模板就行了,因为BC上有道类似的题,自己又学过Manacher算法,结果入坑WA到死
开始写的是判断是否 p[i]-1 <= p[i+p[i]-1]-1,但是没有想到这种情况:5 (5 1) (1 5) (5 1) 1
单靠最长回文半径是不行的,看了网上的解题报告知道,要从极端位置往回挪才行
给我的教训是只会套模板是没用的,要灵活的使用该算法。另外max() 函数似乎很费时间
详细解释 */
/************************************************
* Author :Running_Time
* Created Time :2015-8-11 12:52:49
* File Name :C.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e5 + ;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x7fffffff;
const int MOD = 1e9 + ;
int a[MAXN*], p[MAXN*];
int n; int Manacher(void) {
a[n] = INF + ;
for (int i=n; i>=; --i) {
a[i*+] = a[i]; a[i*+] = INF;
}
a[] = INF + ; n = n * + ;
int id = ; p[] = ;
for (int i=; i<n; ++i) {
if (id + p[id] > i) p[i] = min (p[*id-i], id + p[id] - i);
else p[i] = ;
while (a[i-p[i]] == a[i+p[i]]) p[i]++;
if (id + p[id] < i + p[i]) id = i;
} int mx = ;
for (int i=; i<=n-; i+=) {
if (p[i] - > mx) {
int c = p[i] - ;
while (c > mx && p[i+c] < c) c--;
mx = mx > c ? mx : c;
}
} return mx / * ;
} int main(void) { //HDOJ 5371 Hotaru's problem
int T, cas = ; scanf ("%d", &T);
while (T--) {
scanf ("%d", &n);
for (int i=; i<n; ++i) scanf ("%d", &a[i]);
printf ("Case #%d: %d\n", ++cas, Manacher ());
} return ;
}
Manacher HDOJ 5371 Hotaru's problem的更多相关文章
- hdoj 5371 Hotaru's problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 这道题用到了Manacher算法,首先简单介绍一下Manacher算法: ----------- ...
- Hdu 5371 Hotaru's problem (manacher+枚举)
题目链接: Hdu 5371 Hotaru's problem 题目描述: 给出一个字符串N,要求找出一条N的最长连续子串.这个子串要满足:1:可以平均分成三段,2:第一段和第三段相等,3:第一段和第 ...
- HDU 5371——Hotaru's problem——————【manacher处理回文】
Hotaru's problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 2015 Multi-University Training Contest 7 hdu 5371 Hotaru's problem
Hotaru's problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5371 Hotaru's problem (Manacher,回文串)
题意:给一个序列,找出1个连续子序列,将其平分成前,中,后等长的3段子序列,要求[前]和[中]是回文,[中]和[后]是回文.求3段最长为多少?由于平分的关系,所以答案应该是3的倍数. 思路:先Mana ...
- HDU 5371 Hotaru's problem Manacher+尺取法
题意:给你一个序列,求最长的两段回文子串,要求他们共用中间的一半. 思路:利用Manacher求出p[i]表示的当前位置的最长回文串长度,然后把每一个长度大于等于2的回文串的左区间和右区间分别放到两个 ...
- 【HDOJ 5371】 Hotaru's problem
[HDOJ 5371] Hotaru's problem Manacher算法+穷举/set Manacher算法一好文:http://blog.csdn.net/yzl_rex/article/de ...
- [2015hdu多校联赛补题]hdu5371 Hotaru's problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 题意:把一个数字串A翻过来(abc翻过来为cba)的操作为-A,我们称A-AA这样的串为N-se ...
- Hotaru's problem
Hotaru's problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
随机推荐
- Linux rz的使用
RZ是Linux提供的上传的命令,基于XMODEM/YMODEM/ZMODEM协议. 让我们来测试一下参数吧: 先准备一个文件,就叫test.txt吧,内容如下: one line rz -+ 如果 ...
- Codeforces 628F Bear and Fair Set
题意: 给定若干个上限upto以及集合中在[1,upto]中的元素个数,问是否存在这样的集合使得集合中的元素除以5的余数的个数相等. 分析: 首先可以想到区间的数与其除以5的余数和区间编号分别一一对应 ...
- CLR运行机制
CLR编译器会将我们的代码编译成托管模块(中间IL语言和元数据),托管模块是一个标准的PE32执行文件,或者PE32+执行文件.但是CLR实际不和托管模块一起工作,他会将托管模块合并成程序集,程序集是 ...
- POJ3728 The merchant解题报告
Description There are N cities in a country, and there is one and only one simple path between each ...
- 04-js的运算符
<html> <head> <title>js的运算符学习</title> <meta charset="UTF-8"/> ...
- JSP标准标签库(JSTL)
JSTL:JSP Standard Tag Library:JSP标准标签库 以下内容引用自http://wiki.jikexueyuan.com/project/jsp/standard-tag-l ...
- GNS3模拟的硬件
Hardware emulated by GNS3 Cisco 1700 Series 1700s have one or more interfaces on the motherboard, 2 ...
- Oracle 设置用户密码永不过期
--1.查看用户的proifle,一般是default select username,profile from dba_users; --2.查看概要文件(default)的密码有效期设置 sele ...
- MySQL架构优化实战系列4:SQL优化步骤与常用管理命令
- python的for else语句
Python循环中的else语句 绝大部分编程语言中都有条件判断语句,比如 if … else ,在大部语言中,else 一般只在条件判断语句中出现,与 if 语句配套出现,不过在 Python 中, ...