Go Manacher algorithm of O (n) palindrome string algorithm

Source: Internet
Author: User

here, I introduce a method of O(n) palindrome processing. manacher algorithm .
Original Address:
http://zhuhongcheng.wordpress.com/2009/08/02/ a-simple-linear-time-algorithm-for-finding-longest-palindrome-sub-string/
In fact, the original word is relatively clear, just English, I write a Chinese here.
First: We all know what is called palindrome string, this algorithm to solve is a string of the longest palindrome substring how long. This algorithm can find out the length of the longestpalindrome with each character centered in the time complexity of O(n), in the case of linear time complexity.
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 generally be used' # 'separated. This is very ingenious to the odd-length palindrome string and even-length palindrome unified to consider (see the following example, the palindrome string length is all odd), and then use an auxiliary arrayPrecords information about the longest palindrome string centered on each character. P[ID] is recorded as a characterStr[ID] As the center of the longest palindrome string, whenStr[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 ArraysP:1 2 1 2 3 2 1 2 1 2 1 4 1 2 1 2 1 2 1
There's a good nature here,P[ID]-1is 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.
OK, let's go on. The key question now is how toO(N) to find out in the complexity of timePArray. Just put thisParray to find out, the longest palindrome string can be directly swept through it.
since this algorithm is linear in the past, it is backward-swept. So when we're ready to askP[I],Iof the previousP[J] we've got it. We useMXrecorded inIin the previous palindrome, extend to the right-most position. Simultaneously withIDThis variable is noted to get this optimalMXwhen theIDvalues. (Note: In order to prevent character comparisons from crossing the border, I added the '#' string before adding another special character '$' So my new string subscript is from 1start of)
Well, here we can put a code on it first.

    voidPK () {inti; intMX =0; intID;  for(i=1; i<n; i++)        {            if(MX >i) p[i]= MIN (p[2*id-i], mx-i); ElseP[i]=1;  for(; str[i+p[i]] [= Str[i-p[i]]; p[i]++)                ; if(P[i] + i >mx) {mx= P[i] +i; ID=i; }        }    }

The code is not very short ah, and pretty good to write. Very convenient, remember the algorithm I said above to avoid a lot of unnecessary repetition match it. What does that mean, in fact, this is a code.

  if(MX>I)P[I]= MIN (P[2*ID-I],MX-I); Is the furthest length of the current polygon comparison ,P[i] has a minimum value when mx>i. The core idea of this algorithm is here, why does the P array satisfy such a property ?(The following section is in the form of a picture)

After reading this algorithm, you may find out where this algorithm will be used? In fact, a palindrome string suffix array can also be done. Just the complexity is O(n log n), and generally will not deliberately go card a log n algorithm. But just hdu There is such a problem, you use the suffix array to write how to get T(of course, I write too bad). If you don't believe, you can also try this question.

http://acm.hdu.edu.cn/showproblem.php?pid=3068
also, by the way, a copy AC Code.
http://acm.hust.edu.cn:8080/judge/problem/viewSource.action?id=140283

Go Manacher algorithm of O (n) palindrome string algorithm

Related Article

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.