Problem Description:
The so-called Palindrome string, is the shape of "Ababa" such as the positive sequence and reverse sequence exactly the same string. The longest palindrome substring problem is to find the longest palindrome in all substrings of a string.
Title Link: http://hihocoder.com/problemset/problem/1032
Algorithm Analysis:
Consider all substrings of an odd length, which you can do:
The central position of the enumeration substring I, the longest palindrome substring centered on str[i], the longest palindrome substring centered on the str[I], and finally the largest one in f[I], that is, the length of the longest palindrome substring to be found. The key to the problem is how to solve each f[I]. If each i is centered on str[i], then the complexity of the worst case (shaped like "aaaaaaaaaaaaaa") is O (N2), so it cannot be done. So what should we do? Consider the following question: If str[3...7] is a palindrome string, while str[3...5] is a palindrome string, then str[5...7] is it also must be a palindrome string? The answer is yes. Then we can draw a conclusion: f[I]>=f[2*j-i]. The conclusion is that the following conditions need to be met: J<i and I are within the longest palindrome string (j+f[j]/2>=i) centered on J. In addition, when f[2*j-i] is larger than the left edge of F[J], the above conclusions are wrong and need to be amended to read: f[i]>=min (f[2*j-i], f[j]-2* (I-J)), that is, at this point we can get the minimum value of f[i] can only take str[2*j-i] as the center and no more than f[j] left edge of the palindrome string length. The conclusion of J represents the largest k in the right boundary of a palindrome that satisfies K<i's str[K]. Well, with the above conclusion, when we calculate f[i], we do not have to start with a substring of length 1, but we can start with the minimum value we have, so that the algorithm can end up very quickly even in the case of extreme conditions.
For even-numbered substrings, we can convert the original string to another string by inserting an identical special character into all the adjacent characters in the string, and then use the above algorithm to find all the longest palindrome substrings with an odd length of putting, and finally the result is obtained by simple conversion.
Shown is when F[2*j-i] is not very large (no more than the left edge of F[j]):
Here's My Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 #defineMax_len 10000056 7 Charstr[max_len*2], Tmpstr[max_len];8 intf[max_len*2];9 Ten intMain () One { A intN; -scanf"%d", &n); - while(n--) the { -scanf"%s", tmpstr); - intres =1; - intLen =strlen (TMPSTR); + for(intI=0, j=0; j<len-1; i+=2, ++j) - { +Str[i] =Tmpstr[j]; Astr[i+1] ='#'; at } -str[2*len-2] = tmpstr[len-1]; -str[2*len-1] =' /'; -Len =2*len-1; - -f[0] = f[len-1] =1; in intx =0, Max_r =0; - to for(intI=1; i<len-1; ++i) + { - if(x+f[x]/2>=i) the { *F[i] = min (f[2*x-i], f[x]-2* (I-x)); $ }Panax Notoginseng Else - { theF[i] =1; + } A the for(intj=f[i]/2+1; i-j>=0&&i+j<len; ++j) + { - if(Str[i-j]==str[i+j]) f[i] + =2; $ Else Break; $ } - - if(i+f[i]/2>max_r) the { -x =i;WuyiMax_r = i+f[i]/2; the } - Wu } - About for(intI=0; i<len; ++i) $ { - if(str[i+f[i]/2]=='#') F[i] = f[i]/2; - ElseF[i] = (f[i]+1)/2; - if(f[i]>res) res =F[i]; A } + theprintf"%d\n", res); - } $ return 0; the}
Longest palindrome substring