Horse-drawn vehicles - double palindrome

Topic Link

The meaning of problems: double palindrome is a palindrome string himself, he must be even length, the left and right halves must be even palindromic string string string seeking a palindrome length of the longest double

answer:

  # Double palindrome must be centered, or palindrome string length can not be even. Calculated by the algorithm Manacher m length of each string to the longest palindromic # centered, in the calculation process, the edge extension, while updating the longest string length double palindromic

  Palindromic double string satisfying the condition:

  1. (p [i] -1)% 4 == 0 (i.e. palindromic string length is a multiple of 4)

  How can I tell about two and a half palindrome string string is a palindrome? Palindromic sequence palindrome center i palindromic sequence palindrome radius P [i] (odd number) Thus, the left half of the palindromic sequence is central ip [i] / 2 If the condition p [ip [i] / 2 ]> p [i] (i.e., left palindromic sequence is palindromic sequence palindromic radius larger than the left from the center to the center of the palindrome sequence) satisfies the left half of the palindromic sequence is palindromic sequence

Because it is symmetrical right part is  

  3. centric #

 

Code

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1e6+5;
 4 
 5 char s[maxn],str[maxn];
 6 int l1,l2,p[maxn],ans;
 7 
 8 void init()
 9 {
10     str[0]='$';
11     str[1]='#';
12     for(int i=0;i<l1;i++)
13     {
14         str[i*2+2]=s[i];
15         str[i*2+3]='#';
16     }
17     l2=l1*2+2;
18     str[l2]='*';
19 }
20 int manacher()
21 {
22     int id=0,mx=0,ans=0;
23     for(int i=1;i<l2;i++)
24     {
25         if(mx>i)p[i]=min(p[2*id-i],mx-i);
26         else p[i]=1;
27         for(;str[i+p[i]]==str[i-p[i]];)
28         {
29             p[i]++;
30             if(str[i]=='#' && (p[i]-1)%4==0 && p[i-p[i]/2]>p[i]/2)ans=max(ans,p[i]-1);
31         }
32         if(p[i]+i>mx)
33         {
34             mx=p[i]+i;
35             id=i;
36         }
37     }
38     return ans;
39 }
40 int main()
41 {
42 
43     scanf("%d",&l1);
44     scanf("%s",s);
45     l1=strlen(s);
46     init();
47 
48     printf("%d\n",manacher());
49     return 0;
50 }
View Code

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/j666/p/11519866.html