Manacher algorithm -- Obtain the longest response substring

Source: Internet
Author: User

ManacherAlgorithmIs used to solve the longest substring of a given string, which is the same as the original string after the string is flipped. By adding characters, this algorithm cleverly converts the length of the input back to an even number into an odd number, which simplifies the problem and removes redundant comparisons by taking advantage of the nature of the input back, in this way, the review degree is reduced to O (LEN), and the longest return string centered on each character can be obtained, and the longest return substring can be obtained.

Now, let's briefly describe the algorithm process:

    1. Add special characters (that is, the same character that the original string does not appear, such as '#') between two adjacent characters (including the beginning and end). In this simple step, all the replies are converted to an odd length. This can be used to prove that, if the length of the return string is an even number of N, the number of special characters to be added between them is n-1 + 2, there are n-1 gaps and the beginning and end. Therefore, the length of N + n + 1 = 2 * n + 1 is an even number. Similarly, the length of an odd number is 2 * n + 1 or an odd number. That is to say, we only need to find the return of an odd number of characters without considering the length of an even number.
    2. Assume that the longest echo centered on the Character s [I] is s [I-len [I], I + Len [I], that is to say, Len [I] is the return radius-1. We can see from the length of 2 * n + 1 after adding special characters above, 2 * Len [I] + 1 = 2 * n + 1, that is, Len [I] is an I-centered string with special characters removed, that is, the answer we need.
    3. Now it is the key part. Suppose we know the value of Len [J] (1 <= j <I) and know rightmost = max {J + Len [J]}, there is also the corresponding subscript K, which now has the following situations:
    • Rightmost <I. In this case, it indicates that the characters near I have not been compared or known. Therefore, Len [I] = 0, starting from 0, is compared to the two sides.
    • Rightmost> = I, which indicates that the characters near I have been compared before:
We know K, I, and Len [k] corresponding to K, that is, s [k-len [K], K + Len [k] is a echo, find the I symmetry point 2 * k-I about K, if 2 * k-I-len [2 * k-I]> = k-len [K], that is, the orange text return string on the Left does not exceed the range of K. We immediately know that Len [I]> = Len [2 * k-I], that is to say, the two orange strings are identical, because the string on the right is the flip string on the left, while the orange part is the echo string, which is identical after the flip, So the nature of the ECHO is used, saves a lot of comparisons, we only need to start from Len [I] = Len [2 * k-I], but if 2 * k-I-len [2 * k-I] <k-len [k], that is, the left-side echo string exceeds the range of K. For example, I + Len [2 * k-I] Obviously exceeds K + Len [K]. the excess part is unknown, so we need to compare it again, that is, compare it from Len [rightmost-I]. Therefore, we can find in two cases, you must start from Len [I] = min (LEN [2 * k-I], rightmost-I) until there are differences between the left and right sides, this is the proof of Len [I]'s value complexity: we can find that when Len [2 * k-I] <rightmost-I, that is, the first graph, at this time, because the orange text returns on both sides of the left and right are identical, and the green part is flipped, Len [I] cannot be increased, and no matching is required. The complexity is O (1 ), when Len [2 * k-I]> = rightmost-I, Len [I] = rightmost-I, that is, the comparison starts from rightmost. As rightmost continues to increase, the length (string s length) can be increased at most. The complexity of each comparison is O (1), so the total time complexity is length * O (1), that is, O (length ), that is, the entire algorithm of O (n) is so simple. The template is as follows:
# Include <cstdio> # include <iostream> # include <cstring> using namespace STD; # define n 222222 char s [N], STR [N]; int ans [N]; int manacher (char SRC [], char TMP [], int Len [], char unique, char begin) {int I, J, K, rightmost, N = strlen (SRC), ret = 0; TMP [0] = begin; TMP [1] = unique; for (I = 0; I <n; ++ I) {TMP [I * 2 + 2] = SRC [I]; TMP [I * 2 + 3] = unique;} n = N * 2 + 2; TMP [N] = '\ 0'; for (I = 0; I <n; ++ I) Len [I] = 0; rightmost = k = 0; for (I = 1; I <n; ++ I) {If (rightmost> = I) Len [I] = min (LEN [2 * k-I], rightmost-I); else Len [I] = 0; for (j = Len [I] + 1; TMP [I + J] = TMP [I-j]; ++ J) ++ Len [I]; if (I + Len [I]> rightmost) {rightmost = I + Len [I]; k = I;} ret = max (Ret, Len [I]);} return ret;} int main () {While (~ Scanf ("% s", STR) printf ("% d \ n", manacher (STR, S, ANS, '#', '$ ')); return 0 ;}

Question: http://acm.hdu.edu.cn/showproblem.php? PID = 3068 is a raw template question.

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.