Manacher ' s algorithm horse cart algorithm

Source: Internet
Author: User

This horse-drawn car algorithm Manacher's algorithm is the linear method used to find the longest palindrome string of a character, invented by a man named Manacher in 1975, the greatest contribution of this method is to increase the complexity of time to linear, which is very remarkable. For the palindrome string presumably everyone is not unfamiliar, is reading anti-read all the same string, such as "Bob", "Level", "noon" and so on, so how to find the longest palindrome in a string, you can take each character as the center, looking for a palindrome substring, after traversing the complete array of numbers, You can find the longest palindrome string. But the time complexity of this method is O (n*n), is not very efficient, the following we look at the time complexity of O (n) of the horse-drawn vehicle algorithm.

Because the length of palindrome can be odd, such as "Bob" is an odd form of palindrome, "noon" is even form of palindrome, horse-drawn car algorithm is the first step of preprocessing, the practice is to add a special character around each character, such as adding ' # ', then

Bob--#b #o#b#

Noon-#n #o#o#n#

The advantage of this is that regardless of whether the original string is odd or even, the number of strings received after processing is an odd number, so it can be done together without having to discuss it in a single case. Next we also need and processing after the string t equal length array p, where p[i] denotes the radius of the palindrome string centered on the t[i] character, if p[i] = 1, then the palindrome substring is t[i] itself, then we look at a simple example:

# 1 # 2 # 2 # 1 # 2 # 2 #
1 2 1 2 5 2 1 6 1 2 3 2 1

Because the first and last characters are the # number, and also need to search palindrome, in order to prevent cross-border, we also need to add a non-# character, the actual operation we only need to add a non-# character at the beginning, the end is not added because the end of the string is identified as ' + ', is equal to the default. Through the P array we can find its maximum value and its position, we can determine the longest palindrome substring, then we see how to find the P array, we need to add two auxiliary variable MX and ID, where the ID is the largest palindrome substring center position, MX is the palindrome string can extend to the right side of the position, The most central line of this algorithm is as follows:

P[i] = mx > I? Min (p[2 * id-i], mx-i): 1;

So to speak, if this line of understanding, then the horse-drawn car algorithm is basically no problem, then this line of code apart to see is

If MX > I, then p[i] = min (p[2 * id-i], mx-i)

Otherwise, p[i] = 1

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 are symmetric, the palindrome string centered on s[i] is necessarily contained in a palindrome string centered on S[id], so there must be p[i] = P[j], see.


When P[j] >= mx-i, the palindrome string centered on s[j] is not necessarily completely contained in a palindrome string centered on S[id], but based on symmetry, the two green boxes are surrounded by the same part, that is, a palindrome string centered on S[i], It will expand to the right at least to the MX position, i.e. P[i] >= mx-i. As to whether the post-MX part is symmetrical, it can only be honestly matched.




For the case of MX <= i, p[i] could not be made more assumptions, only p[i] = 1, and then to match.

See the following implementation code:

 #include <vector> #include <iostream> #include <string>using namespace std;string manacher (    string s) {//Insert ' # ' string t = "$#";        for (int i = 0; i < s.size (); ++i) {T + = s[i];    T + = "#";    }//Process t vector<int> P (t.size (), 0);    int mx = 0, id = 0, reslen = 0, rescenter = 0;        for (int i = 1; i < t.size (); ++i) {p[i] = mx > I min (p[2 * id-i], mx-i): 1;        while (T[i + p[i]] = = T[i-p[i]]) ++p[i];            if (MX < i + p[i]) {mx = i + p[i];        id = i;            } if (Reslen < p[i]) {Reslen = P[i];        Rescenter = i; }} return S.substr ((Rescenter-reslen)/2, resLen-1);}    int main () {string S1 = "12212";    cout << manacher (S1) << Endl;    String s2 = "122122";    cout << manacher (S2) << Endl;    string s = "WAABWSWFD"; cout << Manacher (s) << Endl;} 

Manacher ' s algorithm horse cart algorithm

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.