Longest palindrome---hdu3068 (palindrome string manacher algorithm template)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3068

Test instructions is clear: it is to find the length of the longest palindrome in a string s substring; This type of problem uses the Manacher algorithm

Manacher algorithm (copy of the Great God's explanation):

Definition array P[i] denotes I-centric (including I, the character) the length of the palindrome string

The string s is previously swept to the back for (int i=0;i<strlen (s); ++i) to calculate p[i], then the largest p[i] is the longest palindrome string length, then the question is how to seek p[i]?

Since S is previously swept back, it must have been calculated when P[i] is needed p[1]....p[i-1]

Suppose you now scan to the location of I+k and now need to calculate p[i+k]

Defines maxlen as the right-most place to reach in all palindrome strings before the i+k position, that is, Maxlen=p[i]+i;//p[i]+i represents the largest

In two cases:

1.i+k This position is not in the front of any palindrome string, that is, I+k>maxlen, then the initialization p[i+k]=1;//itself is a palindrome string

Then P[i+k] extends around, i.e. while (s[i+k+p[i+k]] = = S[i+k-p[i+k]) ++p[i+k]

2.i+k This position is contained by a palindrome string in front of position I, i.e. maxlen>i+k

In this case P[i+k] is not starting from 1

Because of the nature of palindrome string, we know i+k this position about I and i-k symmetry,

So P[i+k] is divided into the following 3 situations to draw

Black is the palindrome range of I, Blue is the range of I-k palindrome string,

#include <stdio.h>#include<string.h>#include<algorithm>#include<stdlib.h>using namespacestd;Const intN = 1e6+7;CharS[n];intP[n];intManacher (CharS[],intN) {    intIndex =0, MaxLen =0;  for(intI=2; i<n; i++)    {        if(MaxLen >i) p[i]= Min (p[index*2-I.], maxlen-i); ElseP[i]=1;  while(s[i-p[i]]==s[i+P[i]]) P[i]++; if(i+p[i]>maxlen) {MaxLen= i+P[i]; Index=i; }    }    intAns =0;  for(intI=2; i<n; i++) ans=Max (ans, p[i]); returnans-1;}intMain () { while(SCANF ("%s", s)! =EOF) {memset (P,0,sizeof(p)); intLen =strlen (s);  for(intI=len; i>=0; i--) {s[i+i+2] =S[i]; S[i+i+1] ='#'; } s[0] ='$'; intAns = manacher (S,2*len+2); printf ("%d\n", ans); }    return 0;}
View Code

Longest palindrome---hdu3068 (palindrome string manacher algorithm template)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.