Palindrome string, presumably everyone will not be unfamiliar with it?
Palindrome's going to ask for it? Violence once O (n^2) is simple, but when the character length is very long, it will be tle, simple, hash+ two points, its complexity is about O (Nlogn), and manacher algorithm can be in linear time processing the longest palindrome substring.
Let's take a look at the question: http://acm.hdu.edu.cn/showproblem.php?pid=3068
The clever thing about this algorithm is that the odd palindrome string and even-numbered palindrome are considered together. This has always been a problem in the issue of palindrome is more annoying place. The algorithm also has a good place is to take full advantage of the character matching the particularity, avoid a large number of unnecessary duplicate matching. How does that come about?
We require the longest palindrome substring in the string s
Manacher re-constructs a new array, inserting a ' # ' in the middle of every two characters, such as:
It is easy to see that if this happens (for example) in the third character (' # ') city, it will be found that the sweep to the left may appear out of bounds
To prevent the number of indexes from exceeding the array boundary value for character comparisons, you can add a distinguishing character to the first position of the processed string (at index 0) and I like to use ' @ '.
Is the way it has been handled. (The number represents the number of array bits, 3 is s[3])
We now use the P[i] array to represent the palindrome radius centered on the character s[i] (e.g. p[3] = 1).
It is easy to see that the oldest oldest sequence is exactly the length (P[i]-1).
So how do we ask for p[i] arrays?
The steps are as follows:
When processing to [i], we go to find out if there is a number (set as ID) in 0-i to make P[id] + ID > i, set mx = P[id] + ID.
For example, because the red string is a palindrome string, so about the J-symmetric palindrome substring and about I symmetric palindrome substring is exactly the same (two green lines in the picture), and satisfies Mx-i>p[j] when the J's palindrome radius is less than J to MX about J Symmetric left end of the difference, You can initialize P[I]=P[J] at this time. But what if the green part exceeds the ID of the palindrome string?
Such as:
Purple indicates that a palindrome with the center of J is outside the part of the ID-centered palindrome radius (the red part of the figure) because the red string is a palindrome string, so about the J-symmetric palindrome substring and about I symmetric in the MX and MX symmetry points between the palindrome substring is exactly the same (two green lines in the picture), While satisfying the mx-i<=p[j], this time J of the palindrome string radius is greater than or equal to the J-MX on the left end of the symmetry of the difference, at this time can initialize the p[i]=mx-i, out of the part can only be judged. Further increase the radius of the palindrome string of p[i]. (The above two figures are taken from the reference paper)
Summary of initialization p[i] = min (p[j], mx-i);
For outside the Palindrome string part (that is, the red part, we can only one to judge)
i.e. while (S[i + p[i]] = = S[i-p[i]]) p[i] + = 1;
When MX <= i we can only initialize the p[i] to 1 (for which it is unknown and can only be compared by one person).
Enclose the total code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 Const intN =220000+5;6 7 CharS[n];8 9 intP[n];Ten One intN, id, MX; A - voidWork () { -id = mx =0; the intAns =0; -n =strlen (s); - for(inti = n; I >=0; I--){ -s[2* i +2] =S[i]; +s[2* i +1] ='#'; - } +s[0] ='@'; Ap[0] =1; at for(inti =1; I <2* n +2; i + +){ - if(mx > i) p[i] = min (p[2* Id-i], MX-i); - ElseP[i] =1; - while(S[i + p[i]] = = S[i-p[i]]) p[i] + =1; - if(MX < p[i] +i) { -ID =i; inMX = p[i] +i; - } toans = max (ans, p[i)-1); + } -printf"%d\n", ans); the } * $ intMain () {Panax Notoginseng while(SCANF ("%s", s) = =1) work (); - return 0; the}
View Code
Reference: http://blog.csdn.net/pi9nc/article/details/9251455
Longest palindrome substring (manacher algorithm)