Luogu P3805 [template] Manacher algorithm [manacher]

  topic portal

  

Topic description

Given a string S consisting only of lowercase English characters a, b, c...y, z, find the length of the longest palindrome in S.

String length is n

Input and output format

Input format:

 

A string S consisting of a line of lowercase English characters a,b,c...y,z

 

Output format:

 

an integer representing the answer

 

Input and output example

Input example #1:
aaa
Sample output #1:
3

illustrate

String length len <= 11000000

 


 

 

  Analysis: The manacher algorithm template, the algorithm analysis will not be discussed in detail, the five20 boss said it very well, you can refer to his blog .

  Code:

  

 1 //It is made by HolseLee on 30th Apr 2018
 2 //Luogu.org P3805
 3 #include<bits/stdc++.h>
 4 using namespace std;
 5 const int N=2e7+3e6+7;
 6 char s[N],neo[N];int p[N];
 7 int change()
 8 {
 9   int len=strlen(s);
10   int ret=0;neo[ret++]='$';
11   for(int i=0;i<len;i++)
12     neo[ret++]='#',neo[ret++]=s[i];
13   neo[ret++]='#';neo[++ret]='\0';return ret;
14 }
15 int manacher()
16 {
17   int len=change();
18   int ans=-N,id,mx=0;
19   for(int i=1;i<=len;i++){
20     if(i<mx)p[i]=min(p[id*2-i],mx-i);
21     else p[i]=1;
22     while(neo[i-p[i]]==neo[i+p[i]])p[i]++;
23     ans=max(ans,p[i]-1);
24     if(i+p[i]>mx)id=i,mx=i+p[i];
25   }
26   return ans;
27 }
28 int main()
29 {
30   scanf("%s",s);
31   printf("%d",manacher());
32   return 0;
33}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325164655&siteId=291194637