Hotaru's problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1765    Accepted Submission(s): 635

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
 
题目大意:给你t组数据,一个n,下面一行有n个数,问你能形成形如abccbaabc这样的序列长度最长是多少。
 
解题思路:先用manacher处理出来串的所有字符的回文半径。然后枚举第一段跟第二段回文位置i,从i+p[i]-->i进行枚举第二段跟第三段回文位置j。如果以j为回文中心的左端能小于等于i的位置,说明满足要求,更新结果。
 
#include<bits/stdc++.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
const int maxn=1e6;
int a[maxn],p[maxn];
void Manacher(int n){
a[0]=-2;a[n+1]=-1;a[n+2]=-3;
int mx=0,id=0;
for(int i=1;i<=n+1;i++){ //需要处理到n+1
if(i<mx){
p[i]=min(p[id*2-i],mx-i); //这里写的时候写成mx-id,SB了。
}else{
p[i]=1;
}
for(;a[i+p[i]]==a[i-p[i]];++p[i]); //-2,-3防越界
if(i+p[i]>mx){
mx=p[i]+i;
id=i;
}
}
for(int i=1;i<=n+1;++i){
--p[i];
}
}
int main(){
// freopen("1003.in","r",stdin);
// freopen("OUTTTT.txt","w",stdout);
int t,n,cnt=0;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=1;i<=2*n;i+=2){
a[i]=-1;
scanf("%d",&a[i+1]);
} Manacher(2*n);
int maxv=0,j;
for(int i=1;i<=2*n;i+=2){ //2*n
for(j=i+p[i];j-maxv>i;j-=2){ //逆序枚举。manacher算法保证j<=2*n+1
if(j-p[j]<=i){
maxv=j-i;
break;
}
}
}
maxv=3*(maxv/2);
printf("Case #%d: %d\n",++cnt,maxv);
}
return 0;
}

  

最新文章

  1. iOS 实现转盘的效果
  2. 【java基础】面向对象的三大特征---多态
  3. 虚拟机安卓APK
  4. Web 在线文件管理器学习笔记与总结(1)初始文件以及获取首层目录信息
  5. PostgreSQL Replication之第十五章 与Walbouncer 一起工作
  6. Java学习之Java的单例模式
  7. CentOS如何分区
  8. Java正则表达式--网页爬虫
  9. C#将图片转化为黑白图片
  10. The Accumulation of Capital
  11. iOS UITableViewCell点击时子视图背景透明的解决方法
  12. MongoDb在windows下的安装与以auth方式启用服务
  13. 杭电ACM2004--成绩转换
  14. PC/FORTH 变量|常数|数组
  15. python排序
  16. BZOJ 1002 - 轮状病毒 - [基尔霍夫矩阵(待补)+高精度]
  17. oracle 12c直方图收集的增强
  18. 数组&lt;--&gt;变量
  19. angular 项目 error TS2451: Cannot redeclare block-scoped variable &#39;ngDevMode&#39;
  20. JMS概念

热门文章

  1. Delphi和C#数据类型对应表
  2. Windows 安装 mysql-5.7.12-winx64(CommunityServer) 备忘
  3. b,u,i,s,这些被删除的标签以及用来替换他们的标签
  4. mybatis 日期查询datetime
  5. 状压DP【洛谷P1879】 [USACO06NOV]玉米田Corn Fields
  6. DP【洛谷P3135】[USACO16JAN]堡哞Fort Moo
  7. javascript事件委托//就是父级事件给子级
  8. kuangbin专题十六 KMP&amp;&amp;扩展KMP POJ2752 Seek the Name, Seek the Fame
  9. opencv-图片合成视频
  10. [AH2017/HNOI2017]礼物(FFT)