Description and optimization of maximum palindrome string algorithm

Source: Internet
Author: User

background

Recently began to study the algorithm, so in Leetcode do algorithm problem, the fifth question longest palindromic Substring is about palindrome string.

What is a back text string

A palindrome string is a string that is reversed before and after the string is the same as the string. For example: A,aaaa,aba,abba ...

Longest palindrome substring

Requires the longest palindrome substring, it is necessary to traverse each substring, the time complexity is O (N2), to determine whether the string is a palindrome, time complexity is O (N), so that the time complexity of the algorithm is O (N3).

The first thing I thought about was the central extension, the code is as follows:

public static string Getlongestpalindrome (String str) {if (Str.isempty () | | str.length () = = 1) {return        Str          } String longest = str.substring (0, 1); for (int i = 0; i < str.length (); i++) {//Get longest palindrome with center of I String TM              P = Helper (str, I, i);              if (Tmp.length () > Longest.length ()) {longest = TMP;              }//Get longest palindrome with center of I, i+1 tmp = helper (str, I, i + 1);              if (Tmp.length () > Longest.length ()) {longest = TMP;    }} return longest; } private static string helper (string str, int begin, int end) {while (Begin >= 0 && end <= s              Tr.length ()-1 && str.charat (begin) = = Str.charat (end)) {begin--;          end++; The String result = str.substring (begin + 1,End);      return result; }

The time complexity of the central extension method is O (N2).

After writing has been thinking, there is no more powerful way, can be time complexity to Kill to O (N), have always wanted to also unexpectedly, later on the internet searched the legendary manacher algorithm.

Let's look at the code first:

public static int[] Getpalindromelength (String str) {StringBuilder newstr = new StringBuilder ();    Newstr.append ("#");    for (int i = 0; i < str.length (); i++) {Newstr.append (Str.charat (i));    Newstr.append ("#");        } int[] rad = new Int[newstr.length ()];    The right edge of the longest sub palindrome string int right =-1;        The center of the longest sub palindrome string int id =-1;            for (int i = 0; i < newstr.length (); i++) {//define the minimum radius int r = 1;            if (I <= right) {r = math.min (right-i, rad[2 * id-i]); }//Try to get a lager radius while (i-r >= 0 && i + R < Newstr.length (            ) && Newstr.charat (i-r) = = Newstr.charat (i + R)) {r++; }//update the right edge and the center of the longest sub palindrome string if (i + R     -1> right) {           right = i + r-1;            id = i;    } Rad[i] = R;    } return rad; }

First of all, the Manacher algorithm provides a clever way to solve the difference between the length of the odd and the length of the different Palindrome method, in each word Fu Yi insert a original string does not appear in a special character, under normal circumstances with "#". So whether it is ABA type Palindrome or ABBA type palindrome, after inserting special characters, #a #b#a# and #a#b#b#a# length is definitely odd, this solves the above problem.

The Manacher algorithm introduces an auxiliary array to record the information of the longest palindrome string centered on each character, Rad[i] records the longest palindrome string centered on the character Str[i], and when the str[i] is centered, the longest palindrome is extended to both sides rad[i] characters.

Original string: Abbac

New string: #a #b#b#a#c#

Auxiliary Arrays: 12 1 2 5 2 1 2 1 2 1

So how does the manacher algorithm calculate the auxiliary array rad?

We calculate rad[i] from left to right, and when we calculate rad[i], Rad[j] (0<=j<i) has been calculated. We assume that the integer right is the most immediate edge of the current longest palindrome string, and that the center point of the currently longest palindrome substring is the ID, then there are two cases of the current pointer's position I:

The first type: i<=right

So find the symmetrical position of I relative to the center point J (2*id-i), then if rad[j]<right-i, such as:


Then the J-centered palindrome string must be in the ID-centered palindrome string inside, and J and I about the position ID symmetry, by the definition of palindrome string, a palindrome string in turn is a palindrome string, so the length of the palindrome with I as the center is at least the same as the J-centric palindrome, namely rad[i]>= RAD[J]. Because Rad[j]<right-i, so say i+rad[j]<right. by Symmetry Rad[i]=rad[j].

If the rad[j]>=right-i, by symmetry, indicates that the palindrome string centered on I may extend beyond right, and the part that is greater than right we have not yet matched, so to start with a match from the right+1 position until a mismatch occurs, Update right and the corresponding ID as well as rad[i].



Second case: I>right

If I is larger than right, indicating that a palindrome with a midpoint of I has no match at all, this time, you can only honestly one match, the match after completion to update the location of right and the corresponding ID and rad[i].



Summary

1. The Manacher algorithm first skillfully inserts the special characters in all the characters putting, which solves the problem of the different processing methods of the even length and odd length of the back text string.

2. In fact, the complexity of the manacher algorithm is not only O (n), but obviously between O (n) and O (N2), is currently the lowest time complexity of the palindrome substring algorithm.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Description and optimization of maximum palindrome string 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.