Manacher algorithm O (n) to find the longest palindrome substring in a string hdu 3068 (template title)

Source: Internet
Author: User

Manacher (horse-drawn car) algorithm can find the longest palindrome substring in O (n)

The principle of the algorithm is the use of an ID and MX at the time of matching, and the nature of a palindrome string symmetry, so in the complexity of the calculation, the preceding constants do not seem very large

HDU 3068 manacher Algorithm template problem

#include <bits/stdc++.h>using namespacestd;Const intmaxn=3e5+7;CharS[MAXN],STR[MAXN];intLen1,len2,p[maxn],ans;voidinit () {str[0]='#'; str[1]='#';  for(intI=0; i<len1;i++) {Str[i*2+2]=S[i]; Str[i*2+3]='#'; } len2=len1*2+2; STR[LEN2]='*';}voidManacher () {intId=0, mx=0;  for(intI=1; i<len2;i++){        if(mx>i) P[i]=min (p[id*2-i],mx-i); Elsep[i]=1;  for(; Str[i+p[i]]==str[i-p[i]];p [i]++); if(p[i]+i>MX) {MX=p[i]+i; ID=i; }    }    return ;}intMain () { while(~SCANF ("%s", s)) {Len1=strlen (s);        Init ();        Manacher (); Ans=0;  for(intI=1; i<len2;i++) {ans=Max (ans,p[i]); } printf ("%d\n", ans-1); }    return 0;}/*Aaaaabab*/

Manacher algorithm O (n) to find the longest palindrome substring in a string hdu 3068 (template title)

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.