hdu3068 longest palindrome (Manacher algorithm)

Source: Internet
Author: User

Test instructions

The longest palindrome sequence in a string, given a string

Problem Solving Ideas:

Manacher algorithm

Time complexity: O (N)


Code:

#include <cstdio> #include <cstring> #include <algorithm> #define MAXN 110010using namespace Std;char b [Maxn],a[maxn<<1];int p[maxn<<1];int N;int Main () {while    (scanf ("%s", &b[1])!=eof) {        int i;        for (i=1;b[i]!= ' n '; ++i) {            a[(i<<1)]=b[i];            a[(i<<1) +1]= ' # ';        }        a[0]= '? ';        a[1]= ' # ';        N= (i<<1) +2;        a[n]=0;        int maxid,maxl,id;        maxid=maxl=0;        memset (P,0,sizeof (P));        for (int i=1;i<n;++i) {            if (maxid>i)                p[i]=min (p[2*id-i],maxid-i);            else                p[i]=1;            while (A[i+p[i]]==a[i-p[i]])                p[i]++;            if (P[i]+i>maxid)            {                maxid=p[i]+i;                id=i;            }            if (P[I]>MAXL)                maxl=p[i];        }        printf ("%d\n", MaxL-1);    }    return 0;}


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

hdu3068 longest palindrome (Manacher algorithm)

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.