hdu5371 Hotaru's problem
来源:互联网 发布:安卓金手指软件 编辑:程序博客网 时间:2024/05/16 14:39
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2189 Accepted Submission(s): 774
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.
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 than109 , descripting a 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
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.
We guarantee that the sum of all answers is less than 800000.
Sample Input
1102 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 100060int 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;}
0 0
- hdu5371 Hotaru's problem
- HDU5371 Hotaru's problem
- hdu5371 Hotaru's problem (Manacher)
- [Manacher] hdu5371 Hotaru's problem
- 【HDU5371】Hotaru's problem(Manacher + set)
- hdu5371 Hotaru's problem(Manacher算法变形)
- hdu5371 Hotaru's problem manachar回文串算法+枚举
- hdu5371 Hotaru's problem(manacher 算法+枚举)
- Hotaru's problem(hdu5371+Manacher)多校7
- 【最长回文子串】【平衡树】[HDU5371]Hotaru's problem
- 字符串:HDU5371-Hotaru's problem(manacher 的应用)
- hdu5371(2015多校7)--Hotaru's problem(Manacher+线段树)
- hdu5371 Hotaru's problem 2015年多校第七场C题 最长回文串
- HDU 5371 Hotaru's problem
- HDU 5371 Hotaru's problem
- 【HDOJ 5371】 Hotaru's problem
- HDU 5371 Hotaru's problem
- HDU 5371 Hotaru's problem
- 位运算 实现加法
- Ionic:一款用web技术开发类似原生App的神乎其技的html5框架
- PHP:echo print var_dump()和print_r()的区别
- System.getProperty("user.dir"); 获得系统属性
- 第15章 Hibernate的映射技巧
- hdu5371 Hotaru's problem
- 字符串hash函数
- hdoj 1969 Pie
- 技术路线
- [转]使用 Vagrant 打造跨平台开发环境
- 数据持久化 将数据写入到本地 文件管理
- b2
- USB引脚定义以及导线的颜色定义
- "Traditional ViewPager", "FragmentManager & Fragment"的用法