hdu5371 最长回文子串变形(Manacher算法)
Posted wzjhoutai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu5371 最长回文子串变形(Manacher算法)相关的知识,希望对你有一定的参考价值。
http://acm.hdu.edu.cn/showproblem.php?
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.
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 , descripting a sequence.
We guarantee that the sum of all answers is less than 800000.
1 10 2 3 4 4 3 2 2 3 4 4
Case #1: 9
/** hdu5371 最长回文子串变形(Manacher算法) 题目大意:找出一个字符串能够均分为三段,第一段和第三段同样。和第二段互为回文串 解题思路:利用Manacher算出每一个位置的最长回文子串的长度,然后枚举就可以 */ #include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace std; const int maxn=210001; int s[maxn],a[maxn]; int r[maxn],len; void Manancher() { int l=0; a[l++]=-2; a[l++]=-1; for(int i=0;i<len;i++) { a[l++]=s[i]; a[l++]=-1; } a[l]=-3; int mx=0,id=0; for(int i=0;i<l;i++) { r[i]=mx>i?min(r[2*id-i],mx-i):1; while(a[i+r[i]]==a[i-r[i]])r[i]++; if(i+r[i]>mx) { mx=i+r[i]; id=i; } //printf("%d ",r[i]); } // printf("\n"); } int main() { int T,tt=0; scanf("%d",&T); while(T--) { scanf("%d",&len); for(int i=0;i<len;i++) { scanf("%d",&s[i]); } Manancher(); int ans=0; for(int i=1;i<=len*2+1;i+=2) { for(int j=i+r[i]-1;j-i>ans;j-=2) { if(j-i+1<=r[j]) { ans=max(ans,j-i); break; } } } printf("Case #%d: %d\n",++tt,ans/2*3); } return 0; }
以上是关于hdu5371 最长回文子串变形(Manacher算法)的主要内容,如果未能解决你的问题,请参考以下文章
manacher算法 O(n) 求字符串中最长回文子串 hdu 3068(模板题)
HDU 5371 (2015多校联合训练赛第七场1003)Hotaru's problem(manacher+二分/枚举)