Manacher algorithm & Palindrome

Horse-drawn vehicles for Solving the longest substring palindromic, with special focus substring, rather than sequence, time complexity is O (n).

Explain the significance of variables:

Len [i] to memory array position to the i-th position length mx

Location id record time operation (this operation can see the template)

Mx a mark on the longest substring of the rightmost

 

template:

. 1  void the init ()   // This string is used to handle 
2  {
 . 3      Memset (STR, 0 , the sizeof (STR));
 . 4      int K = 0 ;
 . 5      STR [K ++] = ' $ ' ;
 . 6      for ( int I = 0 ; I <len; ++ I)
 . 7          STR [K ++] = ' # ' , STR [K ++] = S [I];
 . 8      STR [K ++] = ' # ' ;
 . 9      len = K;
 10  }
11  int Manacher () // Motomesai长回Ayako comb 
12  {
 13      Len [ 0 ] = 0 ;
 14      int sum = 0 ;
 15      int id, mx = 0 ;
 16      for ( int i = 1 ; i <len; + + i)
 17      {
 18          an if (i <mx) Len [i] = min (mx-i, Len [ 2 * id- i]);
 19          the else Len [i] = 1 ;
 20          the while (str [i-Len [i]] == str [i + Len [i]]) Len [i] ++ ;
 21         if(Len[i]+i>mx)
22         {
23             mx=Len[i]+i;
24             id=i;
25             sum=max(sum,Len[i]);
26         }
27     }
28     return (sum-1);
29 }

 

 

When the i-th character in the center of the palindrome string we ask if i> = mx not optimize this time, is determined (i-1) == (i + 1), (i-2) == (i + 2) .... I have been looking for this

Look at the code is to be 19, then proceeding to 20 lines

 

If i <mx time, this time

This time to see a template question:

 

Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?"

A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not.

The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I've a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I've an even better algorithm!".

If you think you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.

Input

Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string "END" (quotes for clarity).

Output

For each test case in the input print the test case number and the length of the largest palindrome.

Sample Input

abcbabcbabcba
abacacbaaaab
END

Sample Output

Case 1: 13
Case 2: 6

This time to pay attention

I do not know where memset (Len, 0, sizeof (Len)); a timeout caused

still is

. 1  void the init ()
 2  {
 . 3      Memset (STR, 0 , the sizeof (STR));
 . 4      int K = 0 ;
 . 5      STR [K ++] = ' $ ' ;
 . 6      for ( int I = 0 ; I <strlen (S) ; ++ I) resulting from the strlen
 . 7          STR [K ++] = ' # ' , STR [K ++] = S [I];
 . 8      STR [K ++] = ' # ' ;
 . 9      len = K;
 10 }

Correct codes:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<set>
 6 using namespace std;
 7 const int maxn=3000005;
 8 const int INF=0x3f3f3f3f;
 9 const int mod=998244353;
10 char str[maxn],s[maxn];
11 int len,Len[maxn];
12 void init()
13 {
14     memset(str,0,sizeof(str));
15     int k=0;
16     str[k++]='$';
17     for(int i=0;i<len;++i)
18         str[k++]='#',str[k++]=s[i];
19     str[k++]='#';
20     len=k;
21 }
22 int manacher()
23 {
24     Len[0]=0;
25     int sum=0;
26     int id,mx=0;
27     for(int i=1;i<len;++i)
28     {
29         if(i<mx)  Len[i]=min(mx-i,Len[2*id-i]);
30         else Len[i]=1;
31         while(str[i-Len[i]]==str[i+Len[i]]) Len[i]++;
32         if(Len[i]+i>mx)
33         {
34             mx=Len[i]+i;
35             id=i;
36             sum=max(sum,Len[i]);
37         }
38     }
39     return (sum-1);
40 }
41 int main()
42 {
43     int t=0;
44     while(~scanf("%s",s))
45     {
46         //memset(Len,0,sizeof(Len));
47         if(strcmp("END",s)==0) break;
48         len=strlen(s);
49         init();
50         printf("Case %d: %d\n",++t,manacher());
51     }
52     return 0;
53 }
View Code

 

Guess you like

Origin www.cnblogs.com/kongbursi-2292702937/p/11235706.html