The longest palindrome substring template---manacher algorithm. Time complexity O (N)

Source: Internet
Author: User

1#include <vector>2#include <iostream>3#include <algorithm>4#include <stdio.h>5#include <string.h>6 using namespacestd;7 8 #defineN 100010009 intN;Ten intP[n]; One CharS[n], str[n]; A  - #define_min (x, y) ((x) < (y)? ( x):(y)) -  the voidKP () - { -     inti; -     intMX =0; +     intID =0; -      for(I=n; str[i]!=0; i++) +Str[i] =0;//There is no problem with this sentence. Can not live ural1297, such as data: Ababa ABA A      for(i=1; i<n; i++) at     { -         if(MX >i) -P[i] = _min (p[2*id-i], p[id]+id-i); -         Else -P[i] =1; -          for(; str[i+p[i]] [= Str[i-p[i]]; p[i]++) in             ; -         if(P[i] + i >mx) to         { +MX = p[i] +i; -ID =i; the         } *     } $ }Panax Notoginseng  - voidInit () the { +     inti; Astr[0] ='$'; thestr[1] ='#'; +      for(i=0; i<n; i++) -     { $str[i*2+2] =S[i]; $str[i*2+3] ='#'; -     } -N. = n2+2; theS[n] =0; - }Wuyi  the intMain () - { Wu     inti, ans; -     intT; Aboutscanf"%d",&T); $      while(t--) -     { -scanf"%s", s); -n = (int) strlen (s); A init (); + KP (); theAns =0; -          for(i=0; i<n; i++) $             if(p[i]>ans) theAns =P[i]; theprintf"%d\n", ans-1); the     } the     return 0; -}

This algorithm has a very ingenious place, it takes the odd palindrome string and even the palindrome string unified together to consider. This has always been a problem in the issue of palindrome is more annoying place. The algorithm also has a good place is to take full advantage of the character matching the particularity, avoid a large number of unnecessary duplicate matching.

The approximate process of the algorithm is this. Insert a delimiter in the middle of each of the two adjacent characters first, but this delimiter is not present in the original string. Can usually be separated by ' # '. This is very ingenious to the odd-length palindrome string and even-length palindrome unified to consider (see below, the palindrome string length is all odd), and then use an auxiliary array p to record each character as the center of the longest palindrome string information. P[id] records the longest palindrome string centered on the character Str[id], when Str[id] is the first character, the longest palindrome extends to the right P[id] characters.
Original string: w AA bwsw F D
New string: # W # a # a # B # w # s # w # F # d #
Auxiliary array p:1 2 1 2 3 2 1 2 1 2 1 4 1 2 1 2 1 2 1

Here is a good property,p[id]-1 is the length of the palindrome string in the original string (including ' # '). If this is not particularly clear, you can take out the paper to draw a picture, you experience. Of course, everyone here may be different, but I think the general idea should be the same.

The key question now is how to find the P array within the O (n) time complexity . As long as the P array is calculated, the longest palindrome string can be directly swept through it.

So how do you calculate P[i]? the algorithm adds two helper variables (in fact one is enough, two clearer) ID and MX, where the ID represents the location of the largest palindrome substring center, and MX is id+p[id], which is the boundary of the largest palindrome substring.

Then you can get a very magical conclusion, the key point of this algorithm is here: if mx > I, then

P[i] >= MIN (p[2 * id-i], mx-i). This is the string card I have been very long. In fact, if you write it a little more complicated, it will be easier to understand:

j = 2 * id-i, that is, J is the symmetric point about ID.
if (Mx-i > P[j])
P[i] = P[j];
else/* P[j] >= mx-i */
P[i] = mx-i; P[i] >= mx-i, take the minimum value, and then match the update.


When Mx-i > P[j], the palindrome string centered on S[j] is contained in a palindrome string centered on S[id], because I and J symmetry, the palindrome substring centered on s[i] is necessarily contained in a palindrome string centered on S[id], so there must be p[i] = p[j].

When P[j] > mx-i, the palindrome string centered on s[j] is not completely contained in a palindrome string centered on S[id], but is based on symmetry, that is, the palindrome string centered on s[i], which extends right at least to the position of MX, i.e. P[i] >= mx I As to whether the post-MX part is symmetrical, it can only be honestly matched.

Since this algorithm is linear in the past, it is backward-swept. Then when we are ready to beg P[i], the p[j before me] we have got. We use the MX in the palindrome before I to extend to the right-most position. At the same time, use the ID variable to note the ID value when the optimal MX is obtained. (Note: In order to prevent character comparisons, I have added another special character ' $ ' before the string that adds ' # '), so the new string subscript is starting from 1.

Transferred from: http://blog.163.com/zhaohai_1988/blog/static/2095100852012716105847112/

The longest palindrome substring template---manacher algorithm. Time complexity O (N)

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.