the longest palindrome
Time limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total Submission (s): 9188 Accepted Submission (s): 3159
Problem Description
a string s consisting of a lowercase English character a,b,c...y,z is given, and the length of the longest palindrome in S is obtained.
Palindrome is the same as the inverse of the string, such as ABA, ABBA, etc.
Inputinput has multiple sets of case, no more than 120 groups, each set of input is a line of lowercase English characters a,b,c...y,z string s
Between two sets of case separated by a blank line (the empty line is not processed)
String length len <= 110000
Outputeach row is an integer x, corresponding to a set of case, representing the longest palindrome length contained in the string for the group case.
Sample Input
Aaaaabab
Sample Output
43
Sourcemulti-university Training Contest 16-host by NIT
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3068
Problem analysis: The manacher algorithm is the simplest algorithm to solve the longest palindrome substring, the following algorithm is briefly introduced:
The definition array P[i] represents the I-centric (contains I this character) Palindrome string radius Long, the string s from the go to the back sweep to calculate p[i], the last largest p[i] is the longest palindrome string length, the following illustrates how to solve the P array:
Since S is in the past, so the need to calculate p[i] must have been calculated p[1]....p[i-1], assuming now scan to i + K this position, now need to calculate P[i + K], the definition of MAXP is i + K position before all palindrome can extend to the right side of the position, MAXP = P[i] + I
In two cases:
1) i + K This position is not in the front of any palindrome, that is i + K > maxp, then initialize P[i + K] = 1 (that is, itself is a palindrome string) and then will P[i + K] to the left and right extension,
i.e. while (S[i + k + p[i + K]] = = S[i + k-p[i + K]]) P[i + K] + +
2) i + K This position is preceded by the position I as the center of the palindrome string contains, that is Maxp > i + K, so p[i + K] does not start from 1, the nature of the palindrome is known i + K this position about I and i-k symmetry, so p[i + K is divided into 3 cases:
1. I-k Palindrome Range has a part in the p[i] outside, at this time p[i + K] = p[i]-K, at this time P[i + K] cannot be longer, we can disprove, suppose there is a section D after, that is
P[i + K] = P[i]-K + D, then according to Palindrome properties can get p[i] = P[i] + D, and p[i] contradiction, it will not be longer.
2. I-k palindrome range all within p[i], at this time p[i + K] = P[i-k], this is obviously
3. I-k palindrome Range just equal to p[i], at this time p[i + K] = P[i-k], and may continue to extend to the left and right,
i.e. while (S[i + k + p[i + K]] = = S[i + k-p[i + K]]) P[i + K] + +
Combined with all of the above, we can get
P[i + K] = min (P[i-k], P[i]-K)
while (S[i + k + p[i + K]] = = S[i + k-p[i + K]])
P[i + K] + +
The last encounter is a string of even length we want to change its length to an odd number, interspersed with the non-appearing characters, to prevent the array out of bounds, s[0] We also want to set a different character
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;int const MAX = 110005; Char S[max << 1];int p[max << 1];int manacher () {int len = strlen (s), Maxp = 0, ans = 0;for (int i = len; i > = 0; i--) {S[i * 2 + 2] = s[i];s[i * 2 + 1] = ' # ';} S[0] = ' * '; for (int i = 2; i < 2 * len + 1; i++) {if (P[maxp] + maxp > i) p[i] = min (p[2 * maxp-i], P[MAXP] + maxp-i ), else p[i] = 1;while (s[i-p[i] [= S[i + p[i]]) p[i]++;if (P[MAXP] + Maxp < i + p[i]) Maxp = i;if (ans < p[i]) ans = p[ I];} return ans-1;} int main () {while (scanf ("%s", s)! = EOF) printf ("%d\n", Manacher ());}
HDU 3068 longest palindrome (Manacher algorithm)