HDU 3068 (Longest palindrome-manacher) [Template:manacher]

Source: Internet
Author: User

The longest palindromeTime limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 9660 Accepted Submission (s): 3353


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

Manacher's Template

Set radius =p[i] p (' AA ') =1 P (' aba) = ' 2 '

Suppose that the string in the previous substring that was traversed to the far right is the ID at this point (the right-most +1) =mx=i+p[i]

Direct Post paper:

here, let me introduce O ( N ) A method of palindrome string processing. manacher algorithm .
Original Address:
http://zhuhongcheng.wordpress.com/2009/08/02/ a-simple-linear-time-algorithm-for-finding-longest-palindrome-sub-string/
In fact, the original word is relatively clear, just English, I write a Chinese here.
First: We all know what is called palindrome string, this algorithm to solve is a string of the longest palindrome substring how long. This algorithm can find out the length of the longestpalindrome with each character centered in the time complexity of O(n), in the case of linear time complexity.
This algorithm has a very ingenious place, it takes the odd palindrome string and even the palindrome string unified together to consider. 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.
the approximate process of the algorithm is this. Insert a delimiter in the middle of each of the two adjacent characters first, but this delimiter is not present in the original string. Can generally be used' # 'separated. This is very ingenious to the odd-length palindrome string and even-length palindrome unified to consider (see the following example, the palindrome string length is all odd), and then use an auxiliary arrayPrecords information about the longest palindrome string centered on each character. P[ID] is recorded as a characterStr[ID] As the center of the longest palindrome string, whenStr[ID] is the first character, the longest palindrome extends to the right.P[ID] characters.
Original string: w AA bwsw F D
New string: # W# a # a # B# w # s # w # F # d #
Auxiliary ArraysP:1 2 1 2 3 2 1 2 1 2 1 4 1 2 1 2 1 2 1
There's a good nature here,P[ID]-1is the length of the palindrome string in the original string (including '#'). If this is not particularly clear, you can take out the paper to draw a picture, you experience. Of course, everyone here may be different, but I think the general idea should be the same.
OK, let's go on. The key question now is how toO(N) to find out in the complexity of timePArray. Just put thisParray to find out, the longest palindrome string can be directly swept through it.
since this algorithm is linear in the past, it is backward-swept. So when we're ready to askP[I],Iof the previousP[J] we've got it. We useMXrecorded inIin the previous palindrome, extend to the right-most position. Simultaneously withIDThis variable is noted to get this optimalMXwhen theIDvalues. (Note: In order to prevent character comparisons from crossing the border, I added the '#' string before adding another special character '$' and therefore the new string subscript is from1start of)
Well, here we can put a code on it first.


  1. void PK ()
    {
    int i;
    int mx = 0;
    int id;
    for (I=1; i<n; i++)
    {
    if (mx > i)
    P[i] = MIN (P[2*id-i], mx-i);
    Else
    P[i] = 1;
    for (; Str[i+p[i]] = = Str[i-p[i]]; p[i]++)
    ;
    if (P[i] + i > MX)
    {
    MX = p[i] + i;
    id = i;
    }
    }
    }

The code is not very short ah, and pretty good to write. Very convenient, remember the algorithm I said above to avoid a lot of unnecessary repetition match it. What does that mean, in fact, this is a code.
if(MX>I)P[I]= MIN(P[2*ID-I],MX-I);
Is the furthest length of the current polygon comparison ,P[i] has a minimum value when mx>i. The core idea of this algorithm is here, why does the P array satisfy such a property ? (The following section is in the form of a picture)




After reading this algorithm, you may find out where this algorithm will be used? In fact, a palindrome string suffix array can also be done. Just the complexity is O(n log n), and generally will not deliberately go card a log n algorithm. But just hdu There is such a problem, you use the suffix array to write how to get T(of course, I write too bad). If you don't believe, you can also try this question. http://acm.hdu.edu.cn/showproblem.php?pid=3068




#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath> #include <cctype> #include <ctime>using namespace std; #define for (I,n) for (int. i=1;i<=n;i++) #define FORK (I,k,n) for (int. i=k;i<=n;i++) #define REP (I,n) for (int i=0;i<n;i++) #define ForD (I,n) for (int. i=n;i;i--) #define REPD (I,n) for (int. i=n;i>=0;i--) #define FORP (x) for ( int p=pre[x];p; p=next[p]) #define FORPITER (x) for (int &p=iter[x];p; p=next[p]) #define LSON (x<<1) #define Rson ((x<<1) +1) #define MEM (a) memset (A,0,sizeof (a)), #define MEMI (a) memset (A,127,sizeof (a)), #define MEMI (a) memset ( A,128,sizeof (a)), #define INF (2139062143) #define F (100000007) #define MAXN (110000+10) #define SP_CHAR1 (' * ') #define SP_ CHAR2 (' $ ') typedef long Long Ll;ll Mul (ll A,ll b) {return (a*b)%F;} ll Add (ll A,ll b) {return (a+b)%F;} ll Sub (ll A,ll b) {return (a-b+ (a)/f*f+f)%F; void Upd (ll &a,ll b) {a= (a%f+b%f)%F;} Class MansAcher{public:int N;char s[maxn];int p[2*maxn+2];manacher () {n=0; MEM (s) mem (P)}manacher (char *_s) {n=0; MEM (s) if (_s) memcpy (s,_s,sizeof (char) * (strlen (_s) +1)), N=strlen (s); MEM (P)}void mem (char *_s) {n=0; MEM (s) if (_s) memcpy (s,_s,sizeof (char) * (strlen (_s) +1)), N=strlen (s); MEM (p)}char str[maxn*2+2];void work () {str[0]=sp_char1; Rep (I,n) str[2*i+1]=sp_char2,str[2*i+2]=s[i]; STR[2*N+1]=SP_CHAR2; str[2*n+2]= ' + '; n=2*n+2; MEM (p) int mx=0,id=0; for (i,n-1) {if (I&LT;MX) p[i]=min (P[2*id-i],mx-i), while (Str[i-p[i]]==str[i+p[i]]) ++p[i];if (Mx<i+p[i])// MX is the most right end of the identified {Mx=i+p[i];id=i;}}} S;char S[maxn];int Main () {//freopen (". In", "R", stdin);//freopen ('. Out ', "w", stdout); while (~SCANF ("%s", s)) {S.mem (s) ; S.work (); int ans=0; for (i,s.n-1) if (s.str[i]==sp_char1| | S.STR[I]==SP_CHAR2) Ans=max (ans,s.p[i]-1); else Ans=max (ans,s.p[i]-1); Cout<<ans<<endl;} return 0;}









HDU 3068 (Longest palindrome-manacher) [Template:manacher]

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.