The longest palindromeTime
limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 10596 Accepted Submission (s): 3759
Problem description gives a string s that consists only of lowercase English characters a,b,c...y,z, seeking the length of the longest palindrome in S.
Palindrome is the same as the inverse of the string, such as ABA, ABBA, etc.
Input 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
Output an integer x for each row, 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
Source2009 multi-university Training Contest 16-host by NIT
Recommendlcy | We have carefully selected several similar problems for you:1358 1686 3336 3065 3746 longest palindrome string O (n) manacher algorithm, general algorithm, direct enumeration words , each position o (n), has n positions, so use O (n * n) complexity. Consider using a so-called manacher algorithm. In fact, this algorithm is very simple and easy to write.
Defines an array of p,p[i] that represents the length of the largest palindrome string centered on I, i.e. str[i-p[i],i-1] = = Str[i+1,i + P[i]]. We calculate p in turn, a bit of DP meaning, when calculating I, according to the current right end of the maximum value, that is, P[mi] + Mi. If I is at the right end of the P[mi]+mi, direct p[i] = 0; otherwise I am already on the left side of the far right, if, I on the left of p[mi]+ mi, then p[i] = min (mi2-(Mi-p[mi]), p[mi2]); mi2 for i about mi symmetrical position mi2 Because this is to ensure that the maximum length of p[i] is within the palindrome string of I. Here are divided into three kinds of cases mi2 left end of the left end of I, this p[i] directly from the above can be, because, if p[i] than the above, then by the symmetry p[i] to increase, the contradiction mi2 the left end of the right side of I, this p[i] directly from the formula available, because, if p[i] Larger than the above, then by the symmetry of the p[mi2] to increase, the introduction of contradictions
The left end of the mi2 is coincident with the left end of I, which is p[i] directly from the above, and is not determined to be the largest, but also to execute the following while loop.
Since MI is incremented, up to N, and P[mi] + mi are also incremented, so herewhile (i-maxx >= 0 && I+maxx < L2 && Tstr[i-maxx] = = Tstr[i+maxx]) are executed up to n times in total, so the total complexity is the strict O (n) algorithm.
#define N 110050#define M 100005#define maxn 205#define MOD 1000000000000000007void Manacher (char Str[],int len) {char tstr[n+n]; int p[n + N],L2 =0,mi; tstr[l2++] = ' # '; for (int i =0;i<len;i++) {tstr[l2++] = Str[i]; tstr[l2++] = ' # '; } P[0] = 0;mi = 0; for (int i = 1;i<l2;i++) {int mi2 = mi + mi-i; if (mi + p[mi] >= i) p[i] = min (mi2-(Mi-p[mi]), p[mi2]); else p[i] = 0; if (p[i] = = 0 | | MI2-P[MI2] = = Mi-p[mi]) {int maxx = p[i]+1; while (I-maxx >= 0 && I+maxx < L2 && Tstr[i-maxx] = = Tstr[i+maxx]) {maxx++; } P[i] = maxx-1; } if (P[i] + i > p[mi] + mi) mi = i; } int ans =-1; for (int i = 0;i < l2;i++) {ans = max (ans,p[i]); } printf ("%d\n", ans);} int main () {char str[n]; while (SS (str)!=eof) {Manacher (Str,strlen (str)); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu 3068 longest palindrome string O (n) manacher algorithm