HDU 3068-Longest palindrome (Manacher algorithm O (n) to find the longest palindrome string)

Source: Internet
Author: User

Title Address: HDU 3068
A detailed description of the algorithm: Manacher algorithm

#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>#pragma COMMENT (linker, "/stack:102400000,102400000")using namespace STD;typedef Long LongLL;Const intinf=0x3f3f3f3f;Const DoublePi=ACOs(-1.0);Const Doubleesp=1e-6;Const intmaxn=110010;CharSTR[MAXN];//Raw stringChars[maxn*2];//String after Manacher processingintp[maxn*2];//Save palindrome radius, the length of each palindrome string must be an odd numberintNvoidManacher () {intI for(i=1; str[i]!=' + '; i++) {s[i*2]=str[i]; s[i*2+1]=' # '; } s[0]=' $ '; s[1]=' # '; n=i*2+2; s[n]=' + ';//Control End    intMaxl,id; Id=maxl=0;memset(P,0,sizeof(p)); for(intI=1; i<n; i++) {if(p[id]+id>i) P[i]=min (p[2*ID-I],P[ID]+ID-I);Elsep[i]=1; while(S[i+p[i]]==s[i-p[i]]) p[i]++;if(P[i]+i>p[id]+id)        {id=i; }if(P[I]&GT;MAXL)    Maxl=p[i]; }printf("%d\n", maxl-1);}intMain () { while(~scanf('%s ', &str[1]) {manacher (); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3068-Longest palindrome (Manacher algorithm O (n) to find the longest palindrome string)

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.