Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 2189    Accepted Submission(s): 774

Problem Description
Hotaru Ichijou recently is addicated to math problems. Now she is playing with N-sequence.

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.
 
Input
There are multiple test cases. The first line of input contains an integer T(T<=20), indicating the number of test cases. 



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.
 
Output
Each case contains only one line. Each line should start with “Case #i: ”,with i implying the case number, followed by a integer, the largest length of N-sequence.



We guarantee that the sum of all answers is less than 800000.
 
Sample Input
1
10
2 3 4 4 3 2 2 3 4 4
 
Sample Output
Case #1: 9
这题能够用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&#39;s problem的更多相关文章

  1. Hotaru&#39;s problem(hdu5371+Manacher)多校7

    Hotaru's problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. 【HDOJ 5371】 Hotaru&#39;s problem

    [HDOJ 5371] Hotaru's problem Manacher算法+穷举/set Manacher算法一好文:http://blog.csdn.net/yzl_rex/article/de ...

  3. hdu 5371 Hotaru&#39;s problem【manacher】

    题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=5371 题意: 给出一个长度为n的串,要求找出一条最长连续子串.这个子串要满足:1:能够平均分成三段 ...

  4. HDU 5371 Hotaru&#39;s problem(Manacher算法+贪心)

    manacher算法详见 http://blog.csdn.net/u014664226/article/details/47428293 题意:给一个序列,让求其最大子序列,这个子序列由三段组成, ...

  5. HDU 5371(2015多校7)-Hotaru&#39;s problem(Manacher算法求回文串)

    题目地址:HDU 5371 题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...

  6. HDU 5371 (2015多校联合训练赛第七场1003)Hotaru&#39;s problem(manacher+二分/枚举)

    pid=5371">HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分同样,第一部分与第二部分对称. 如今给你一个长为n(n<10^5)的序 ...

  7. [2015hdu多校联赛补题]hdu5371 Hotaru's problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 题意:把一个数字串A翻过来(abc翻过来为cba)的操作为-A,我们称A-AA这样的串为N-se ...

  8. HDU5371 Hotaru's problem

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  9. hdu5371 Hotaru's problem

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission ...

随机推荐

  1. 网站推广--Html关键词代码解说

    html的meta标签 1. meta标签是内嵌在你网页中的特殊html标签,包含着你有关于你网页的一些隐藏信息.Meat标签的作用是向搜索引擎解释你的网页是有关哪方面信息的.对于高级的搜索引擎来说, ...

  2. pjax

    下载地址: https://github.com/defunkt/jquery-pjax/find/master 使用方法: 0.先引入jquery和jquery.pjax.js 1.父页面定义区域d ...

  3. swift class type isa-swizzling

    class 是引用类型,生成的实例分布在 Heap(堆) 内存区域上,在 Stack(栈)只存放着一个指向堆中实例的指针.因为考虑到引用类型的动态性和 ARC 的原因,class 类型实例需要有一块单 ...

  4. 用Go向MySQL导入.csv文件

    今天来更新一个很少碰到,但碰到了又让人十分蛋疼的问题——Go语言中执行MySQL的load data local infile语句报local file 'xxx' is not registered ...

  5. C# 支持多线程

    C# 支持多线程并行执行程序 .一个程序由一个单线程开始,该单线程由CLR和操作系统创建而成,并具有多线程创建额外线程的功能. .创建线程的方法 2.1 通过Thread类来创建线程. ThreadS ...

  6. KMP中next数组的理解与应用

    理解 1.next数组一直往前走 next数组一直往前走,得到的所有前缀也是当前主串的后缀,当然了,也是当前主串的前缀. 2.周期性字符串 1.周期性字符串$\Leftrightarrow n \,\ ...

  7. ZXing.dll 生成二维码 C# winform net4.5

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. Spring boot 控制台打印sql

    在application.ym中加入: logging: level: com.wechat.cwbt.dao : debug 发现无效 在log4j.properties中加入: log4j.log ...

  9. [Luogu] P4460 [CQOI2018]解锁屏幕

    题目背景 使用过Android 手机的同学一定对手势解锁屏幕不陌生.Android 的解锁屏幕由3X3 个点组成,手指在屏幕上画一条线,将其中一些点连接起来,即可构成一个解锁图案.如下面三个例子所示: ...

  10. Mybatis的一级二级缓存

    Mybatis提供了缓存机制,可以减轻数据库的压力,提高性能 Mybatis的缓存分为两级:一个是一级缓存,一个二级缓存 一级缓存:即默认使用的缓存SqlSession级别的缓存,只在sqlsessi ...