hihocoder#1032 Longest palindrome substring

Source: Internet
Author: User

Original title Address

This problem cleverly utilizes the information of the previous palindrome string to reduce the amount of palindrome calculation in the back.

For example, the following string (the circle represents a character) assumes that a character's palindrome is known (the two circles connecting the curve represent two identical characters)

Think: How can this help with the calculation of the palindrome string behind it?

A slap does not ring, to see what the effect of two palindrome is known:

Does it mean something? To be more intuitive, we give curves and Dianran colors:

You don't understand what you're saying? Okay, let's mark the alphabet:

This is very obvious, it is known that the center point in I and the center point in the J of the two palindrome string, now requires the center point in K's palindrome, we can easily know that the length of the palindrome is at least 5(3 blue dots + 2 orange dots).

Note that this is only a lower bound, the purpose is to reduce the subsequent determination of the work of palindrome string.

F[i] Indicates the length of a palindrome with the center point of I, then there is f[k] >= 5

In principle, all known palindrome strings before K can be used to calculate the lower bound, but after all we calculate a lower bound, there is no need to be very precise. So just based on an intuitive idea to decide which point to choose as the basis for the calculation. That is, select the i + f[i]/2 The largest I will be OK. Why is this number?

Assuming there are two candidates, I and I ', they correspond to the Palindrome case as shown.

Should we use I or I? I think normal people would choose to use I instead of I '.

and the above formula (i + f[i]/2) refers to the right boundary of a palindrome string, that is to say, using this I to calculate "should" result is not bad.

OK, I'm sure, what about J? Because we know that a slap doesn't ring. In fact, the choice of J is very simple, just the k along the I symmetry on the line:

We have f[k] ' = f[j].

Remember, F[k] ' is just a nether of f[k], so to get f[k] really how much, or need to continue to judge the palindrome string, but the nether range is not required, which reduces the amount of work to judge Palindrome string.

hihocoder#1032 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.