hdu5371 Hotaru's problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2189 Accepted Submission(s): 774
Let's define N-sequence, which is composed with three parts and satisfied with the following condition:
1. the first part is the same as the thrid part,
2. the first part and the second part are symmetrical.
for example, the sequence 2,3,4,4,3,2,2,3,4 is a N-sequence, which the first part 2,3,4 is the same as the thrid part 2,3,4, the first part 2,3,4 and the second part 4,3,2 are symmetrical.
Give you n positive intergers, your task is to find the largest continuous sub-sequence, which is N-sequence.
For each test case:
the first line of input contains a positive integer N(1<=N<=100000), the length of a given sequence
the second line includes N non-negative integers ,each interger is no larger than 109 ,
descripting a sequence.
We guarantee that the sum of all answers is less than 800000.
10
2 3 4 4 3 2 2 3 4 4
这题可以用Manacher算法做,因为题目要找的是三段(第一段和第二段对称,第二段和第三段对称),其实就是两个连在一起的回文串,我们可以先用Manacher算法初始化各个点的p[i]值(即可以向右延伸的最大距离,包括本身,这时已经加入了-1代替算法中的'#',-2代替算法中的'$'),然后对于每个i,枚举j(j属于1~p[i]-1),如果i+j-p[i+j]+1<=i,那么说明i,j可以分别作为第一、二段的点和第二、三段的点)。
这里有个优化,因为枚举时满足条件的只有'#'(即'-1’),所以我们可以使i,j每次变化2.
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 100060
int a[maxn],b[2*maxn],p[2*maxn];
int main()
{
int n,m,i,j,T,mx,idx,maxx,num1=0;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
if(n<3){
printf("0\n");continue;
}
b[0]=-2;
b[1]=-1;
for(i=0;i<n;i++){
b[i*2+2]=a[i];
b[i*2+3]=-1;
}
n=2*n+2;mx=0;
for(i=0;i<n;i++){
if(i<mx){
p[i]=min(p[idx*2-i],mx-i);
}
else p[i]=1;
while(b[i-p[i]]==b[i+p[i]]){
p[i]++;
}
if(mx<i+p[i]){
mx=i+p[i];
idx=i;
}
}
maxx=0;
for(i=3;i<n;i+=2){
for(j=p[i]-1;j>=1;j-=2){
if(j<maxx)break;
if(i+j-p[i+j]+1<=i){
maxx=max(maxx,j);break;
}
}
}
num1++;
printf("Case #%d: ",num1);
printf("%d\n",maxx/2*3);
}
return 0;
}
hdu5371 Hotaru's problem的更多相关文章
- [2015hdu多校联赛补题]hdu5371 Hotaru's problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 题意:把一个数字串A翻过来(abc翻过来为cba)的操作为-A,我们称A-AA这样的串为N-se ...
- HDU5371 Hotaru's problem
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- [hdu5371 Hotaru's problem]最大回文半径
题意:在一个字符串里面找最长的[A][B][A]子串,其中[A][B]是回文串,[A]和[B]的长度相等 思路:[A][B]是回文串,所以[B][A]也是回文串.先预处理出每个点的最大回文半径Ri,枚 ...
- 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处理回文】
Hotaru's problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- Hdu 5371 Hotaru's problem (manacher+枚举)
题目链接: Hdu 5371 Hotaru's problem 题目描述: 给出一个字符串N,要求找出一条N的最长连续子串.这个子串要满足:1:可以平均分成三段,2:第一段和第三段相等,3:第一段和第 ...
- Manacher HDOJ 5371 Hotaru's problem
题目传送门 /* 题意:求形如(2 3 4) (4 3 2) (2 3 4)的最长长度,即两个重叠一半的回文串 Manacher:比赛看到这题还以为套个模板就行了,因为BC上有道类似的题,自己又学过M ...
- 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) ...
- hdu5371 Hotaru's problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission ...
随机推荐
- 一文搞懂MySQL前缀索引
引入 通常在开发中我们需要定义字符串类型的字段,例如用户名或者用户邮箱等. 假设我们在维护一个用户登录系统,用户表的定义: create table User( ID bigint unsigned ...
- SQLI-LABS复现通关
sql-lab 复现通关(深入学习) less-1 基于错误的单引号字符串 - 正常访问 127.0.0.1/?id=1 - 添加 ' 返回报错信息:You have an error in your ...
- UVA - 185 Roman Numerals
题目链接: https://vjudge.net/problem/UVA-185 思路: 剪枝.回溯 注意回溯的时候,是从当前点的下一个开始,而不是从已经遍历的个数点开始!!不然回溯有问题! 思路参考 ...
- 2V升3V芯片,输入2V输出3V可达1A
PW5328B是一个恒定频率, 6引脚 SOT23电流模式升压转换器,用于小型低功耗应用. PW5328B的开关频率为 1.2MHz,允许使用微小的.低成本的电容器和电感器.内部软启动导致小涌流和延长 ...
- PAT练习num1-害死人补偿命的3n+1猜想
卡拉兹(Callatz)猜想: 对任何一个正整数 n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把 ( 砍掉一半.这样一直反复砍下去,最后一定在某一步得到 n=1.卡拉兹在 1950 年的世界 ...
- CSS响应式布局学习笔记(多种方法解决响应式问题)
在做web开发的工作中,会遇到需要我给页面根据设计的要求,进行响应式布局,这里跟大家分享下我对于响应式布局的解决方法: 我主要利用的是CSS3 媒体查询,即media queries,可以针对不同的媒 ...
- shell批量解压源码包
有时候部署环境有很多安装包,如果一个一个地解压缩实在太麻烦了,可以用shell批量进行解压缩.命令如下: [root@localhost ~]# vi tar.sh #! /bin/bash #标称是 ...
- logging philosophy 日志哲学
Go kit - Frequently asked questions https://gokit.io/faq/ Logging - Why is package log so different? ...
- Profile Guided Optimization Link Time Optimization
https://github.com/python/cpython Profile Guided Optimization PGO takes advantage of recent versions ...
- Python基础(函数)
为什么要用函数? 解决代码重用问题 统一维护 程序的组织结构清晰,可读性强函数:先定义后使用1.内置函数 built-in function eg:sum max len2.自定义函数:定义有参函数 ...