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;
}

最新文章

  1. URL重写无效
  2. js中的eval()和catch()
  3. C++中的异常处理(一)
  4. Gunicorn + Django 部署
  5. jsonp跨域原理
  6. json转csv
  7. 使用grep要注意的地方
  8. struts2的配置和使用
  9. C#面试题总结——程序设计基础
  10. 10-3[RF] feature selection
  11. IOS 学习笔记(5) 控件 文本视图(UITextView)的使用方法
  12. 手动新建MVC控制器和视图,以及数据显示的问题
  13. Exception、Thorow、Throws、TryCatch
  14. sqlloader 往数据库导数据提示数据文件的字段超出最大长度
  15. Go 语言和 Scala 语言对比
  16. Mysql获取最大自增ID(auto_increment)的五种方式及其特点
  17. Hadoop &quot;Cannot create directory .Name node is in safe mode.&quot;解决方案
  18. avg(xxxxxx)什么时候能独自出现?
  19. SWT开发工具
  20. 一位老手关于HTML5的见解

热门文章

  1. SCHTASKS /CREATE
  2. 【My First Blog】评近期国产烂片-《何以笙箫默》
  3. swift 与 NSObject
  4. CREATE DOMAIN - 定义一个新域
  5. 富文本编辑器复制Wod字体问题
  6. 第2节 mapreduce深入学习:14、mapreduce数据压缩-使用snappy进行压缩
  7. Python学习笔记(2)——Python的函数、模块、包和库
  8. copy on write
  9. myBatis查询报错 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
  10. Try, throw和catch用法