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's problem的更多相关文章

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

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

  2. HDU5371 Hotaru's problem

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

  3. [hdu5371 Hotaru's problem]最大回文半径

    题意:在一个字符串里面找最长的[A][B][A]子串,其中[A][B]是回文串,[A]和[B]的长度相等 思路:[A][B]是回文串,所以[B][A]也是回文串.先预处理出每个点的最大回文半径Ri,枚 ...

  4. Hotaru's problem

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

  5. HDU 5371——Hotaru's problem——————【manacher处理回文】

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

  6. Hdu 5371 Hotaru's problem (manacher+枚举)

    题目链接: Hdu 5371 Hotaru's problem 题目描述: 给出一个字符串N,要求找出一条N的最长连续子串.这个子串要满足:1:可以平均分成三段,2:第一段和第三段相等,3:第一段和第 ...

  7. Manacher HDOJ 5371 Hotaru's problem

    题目传送门 /* 题意:求形如(2 3 4) (4 3 2) (2 3 4)的最长长度,即两个重叠一半的回文串 Manacher:比赛看到这题还以为套个模板就行了,因为BC上有道类似的题,自己又学过M ...

  8. 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) ...

  9. hdu5371 Hotaru&#39;s problem

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

随机推荐

  1. 【Oracle】用sqlplus登录的各种方式

    1.本地登录 sqlplus / as sysdba 2.账号密码登录 sqlplus user/passwd 3.选择实例登录 sqlplus user/passwd@实例名   例如 sqlplu ...

  2. leetcode 473. 火柴拼正方形(DFS,回溯)

    题目链接 473. 火柴拼正方形 题意 给定一串数,判断这串数字能不能拼接成为正方形 思路 DFS,但是不能每次从从序列开始往下搜索,因为这样无法做到四个边覆盖不同位置的值,比如输入是(5,5,5,5 ...

  3. 在Firefox上使用Chrome的crx扩展程序

    假如你喜欢使用Firefox火狐浏览器,可是发现有个很喜欢很想用的扩展只发布了支持Chrome的crx格式--Firefox从57版以后使用了WebExtension API作为新附加组件的开发标准, ...

  4. [Usaco2008 Feb]Line连线游戏

    题目描述 Farmer John最近发明了一个游戏,来考验自命不凡的贝茜.游戏开始的时 候,FJ会给贝茜一块画着N (2 <= N <= 200)个不重合的点的木板,其中第i个点 的横.纵 ...

  5. Java并发编程常识

    这是why的第 85 篇原创文章 写中间件经常要做两件事: 1.延迟加载,在内存缓存已加载项. 2.统计调用次数,拦截并发量. 就这么个小功能,团队里的人十有八九写错. 上面这句话不是我说的,是梁飞在 ...

  6. 3、wait和waitpid

    1. 函数介绍 wait函数:调用该函数使进程阻塞,直到任意一个子进程结束,或者该进程接收到了一个信号为止,如果该进程没有子进程或该进程的子进程已经结束,wait函数立即返回. waitpid函数:与 ...

  7. WIFI 国家码和信道划分

    前言 网上百度了很多资料,都没有找到国家码对应支持哪些信道的资料,无奈只能qiang到谷歌,分享给大家完整的WIFI 国家码和信道划分. 安卓WIFI国家码的影响 android中设置wifi国家码的 ...

  8. Linux下运行java报错:Error: Could not find or load main class SocketIOPropertites

    [root@node01 testfileio]# javac SocketIOPropertites.java && java Soc ketIOPropertitesError: ...

  9. Dubbo 总结:关于 Dubbo 的重要知识点

    一 重要的概念 1.1 什么是 Dubbo? Apache Dubbo (incubating) |ˈdʌbəʊ| 是一款高性能.轻量级的开源Java RPC 框架,它提供了三大核心能力:面向接口的远 ...

  10. JavaScript基础知识-基本概念

    typeof操作符 typeof 操作符返回一个字符串,表示未经计算的操作数的类型. // 数值 typeof 37 === 'number'; typeof 3.14 === 'number'; t ...