The longest palindrome substring manacher algorithm

Source: Internet
Author: User

In computer science, the problem with the longest palindrome or longest symmetric factor is to find one of the longest contiguous substrings in a string, which must be a palindrome. For example "Banana" the longest palindrome substring is "Anana". The longest palindrome string is not guaranteed to be unique, for example, in the string "Abracadabra", there are no more than three palindrome substrings, but there are two back text string lengths are three, is "Ada" and "ACA". In some applications it is necessary to return all the longest palindrome substrings (all strings are palindrome and cannot be extended to larger palindrome strings) instead of returning one or the largest palindrome string length.

[Manacher (1975)] found a linear time algorithm that can list all palindrome starting from the head of a string in a given string. And, Apostolico, Breslauer & Galil (1995) found that the same algorithm can find all the largest palindrome strings in any position, and the time complexity reading is linear. Therefore, they provide a method of solving the longest palindrome substring with linear time complexity. Alternative linear time resolution jeuring (1994), Gusfield (1997), is provided based on the suffix tree (suffix trees).

It is easy to think of a solution that iterates through all the strings that may be the longest palindrome string, selecting the longest one from there. This solution can be expanded by iterating through each character that may be centered, with the time complexity O (n ^ 2). The manacher algorithm described below can solve the longest palindrome substring in the time complexity of O (n).

The core of the Manacher algorithm is to record the right boundary of the longest palindrome extending to the right, and to simplify and transform the problem by using the symmetry of palindrome string.

Firstly, in order to eliminate the effect of parity, a character that does not appear in the original string is introduced in the original manacher to eliminate the parity effect of string length.

Use an array of p[i] to record the length of the left/right expansion of the longest palindrome string centered on the character S[i] (including s[i), that is, the length of the palindrome string "folded".

S # 1 # 2 # 2 # 1 # 2 # 3 # 2 # 1 #
P 1 2 1 2 5 2 1 4 1 2 1 6 1 2 1 2 1
(as you can see, P[i]-1 is exactly the total length of the palindrome string in the original P.S)

It is best to remember that the right side of the border, with its center ID, is:

if (p[2 * Id-i] < Right-i + 1) p[i] = p[2 * Id-i]

else P[i] > p[2 * id-i]

If the radius of the symmetry point is less than right-i + 1, then the radius of I and its equality, if greater than, then we need to further judge.

    String Manacher (String s) {StringBuffer sb = new StringBuffer ();        Sb.append (' # ');            for (int i = 0; i < s.length (); i++) {Sb.append (S.charat (i));        Sb.append (' # ');        } int[] m = new int[sb.length ()];        M[0] = 1;        int id = 0;        int right = 0; for (int i = 1; i < Sb.length (), i++) {m[i] = right > I?            Math.min (m[2 * id-i], right-i + 1): 1; while (i >= m[i] && (i + m[i]) <= sb.length ()-1 && sb.charat (i-m[i]) = = Sb.charat (i + m[i]) m[i            ]++;                if (i + m[i]-1 > right) {id = i;            right = M[i];        }} int maxR = m[0];        int index = 0;                for (int i = 0; i < m.length; i++) {if (M[i] > MaxR) {MaxR = M[i];            index = i;        }} String res = "";       for (int i = INDEX-MAXR + 1; i < index + maxR-1; i++) {     if (Sb.charat (i) = = ' # ') continue;        else res + = Sb.charat (i);    } return res; }

The longest palindrome substring manacher 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.