Data structure--manacher algorithm (longest palindrome substring)

Source: Internet
Author: User

Find its longest palindrome in a string

May be odd palindrome may also be even palindrome, to consider a comprehensive

Brute Force solution: (Because there is the problem of odd palindrome and even palindrome, so it is not easy to find, there is a trick is to want to add a symbol between each character in the string (any symbol, can also be a symbol in the string), and then at each location to start expanding at each end)

The answer is maximum value/2

Manacher algorithm: A character is also added between each string in a string

Palindrome Diameter: The maximum length to be extended from one position to the sides

1. Palindrome radius array: arr[], the length of the palindrome radius which can be enlarged at each position center

2. Right Palindrome right boundary R: All palindrome radius, the most right position

Start position is-1

3. Palindrome right boundary of the center of the current palindrome right boundary reached the far right, is the location of the Center for the expansion

Algorithm:

1. The current position is I possibility 1:i is not in the palindrome right boundary R, then uses the violent expansion

Possibility 2:i in the palindrome right boundary R, and I about the palindrome right center of Symmetry position I ' palindrome Center radius all in L and R inside

The radius of the Palindrome area at the I position is the same as I '

        

Possibility 3:

Possibility 4:

Data structure--manacher algorithm (longest palindrome substring)

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.