1112 human race, longest palindrome common algorithm discussion

Source: Internet
Author: User

PS. This paste most of the text and code from the Internet, I just collated the next

S= "c A b a" then s ' = "a B a C", in which case the longest common substring of s and S ' is ABA. No errors.

But when s= "Abacdfgdcaba", then S ' = "Abacdgfdcaba". So the longest common substring of s and S ' is ABACD. It is obvious that ABACD is not the longest palindrome string of s, it is not even a palindrome.

It is now clear why the longest palindrome string cannot be transformed into the longest common substring problem. When the original string s contains a non-palindrome string of the inverse sequence, the longest common substring of the solution is not correct. As in the previous example, S contains both ABACD and ABACD crossdress Cdaba, and ABACD is not a palindrome, so the method of transforming into the longest common substring cannot succeed. Unless, every time we find the longest common substring, we check to see if the substring is a palindrome, and if so, the substring is the longest palindrome of the original string, and if not, go to the next second public substring, and so on.

The longest palindrome string has many methods, namely 1 violence Law, 2 dynamic programming, 3 from the center extension method, and 4 famous Manacher algorithm. I will describe several methods below.

Method One violence law

Traversing each substring of the string s, to determine whether the substring is a palindrome, is a palindrome to see if the length is greater than the maximum length of maxlength. The method of traversing each substring to O (N2), to determine whether each substring is a palindrome time complexity is O (N), so the total time complexity of the profiteering method is O (N3).

Method Two dynamic programming time complexity O (N2), Spatial complexity O (N2)

Dynamic programming is the evolutionary version of the violence law, and we don't have to recalculate every substring to see if it's a palindrome. We can record some of the things we need, we can determine at O (1) time that the substring is not a palindrome. This saves the time complexity of O (N) compared to the brute force method Oh, hey, actually optimization is very simple.

P (i,j) is 1 o'clock to represent the string Si to SJ is a palindrome, for 0 o'clock to represent the string Si to SJ is not a palindrome.

P (i,j) = P (i+1,j-1) (if s[i] = S[j]). This is the state transition equation for dynamic programming.

P (i,i) = 1,p (i,i+1) = if (s[i]= s[i+1])

stringLONGESTPALINDROMEDP (strings) {intn =s.length (); intLongestbegin =0; intMaxLen =1; BOOLtable[ +][ +] = {false};  for(inti =0; I < n; i++) {Table[i][i]=true;//Pre-initial initialization  }   for(inti =0; I < n1; i++) {    if(S[i] = = s[i+1]) {table[i][i+1] =true;//Pre-initial initializationLongestbegin=i; MaxLen=2; }  }   for(intLen =3; Len <= N; len++) {     for(inti =0; I < n-len+1; i++) {      intj = i+len-1; if(S[i] = = S[j] && table[i+1][j-1]) {Table[i][j]=true; Longestbegin=i; MaxLen=Len; }    }  }  returns.substr (Longestbegin, maxlen);}
View Code

Method three-Center extension method

This algorithm idea is actually very simple, the time complexity is O (N2), the space complexity is only O (1). is to the given string s, respectively, in the string s of each character C as the center, extending to both sides, the length of a palindrome substring centered on the character C is recorded. One thing to note, however, is that a palindrome may be a b A or a b b.

stringExpandaroundcenter (stringSintC1,intC2) {  intL = C1, r =C2; intn =s.length ();  while(L >=0&& r <= N-1&& S[l] = =S[r]) {L--; R++; }  returnS.SUBSTR (L +1, r-l-1);}stringLongestpalindromesimple (strings) {intn =s.length (); if(n = =0)return ""; stringLongest = S.substr (0,1);//a single char itself is a palindrome   for(inti =0; I < n1; i++) {    stringP1 =Expandaroundcenter (S, I, I); if(P1.length () >Longest.length ()) Longest=P1; stringP2 = Expandaroundcenter (s, I, i+1); if(P2.length () >Longest.length ()) Longest=P2; }  returnlongest;}
View Code

method Four the legendary Manacher algorithm. Time complexity O (N)

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 usually be separated by ' # '. This is very ingenious to the odd-length palindrome string and even-length palindrome unified to consider (see below, the palindrome string length is all odd), and then use an auxiliary array p to record each character as the center of the longest palindrome string information. P[id] records the longest palindrome string centered on the character Str[id], when Str[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 array p:1 2 1 2 3 2 1 2 1 2 1 4 1 2 1 2 1 2 1

Here is a good property,p[id]-1 is 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.

The key question now is how to find the P array within the O (n) time complexity . As long as the P array is calculated, the longest palindrome string can be directly swept through it.

So how do you calculate P[i]? the algorithm adds two helper variables (in fact one is enough, two clearer) ID and MX, where the ID represents the location of the largest palindrome substring center, and MX is id+p[id], which is the boundary of the largest palindrome substring.

Then you can get a very magical conclusion, the key point of this algorithm is here: if mx > I, then

P[i] >= MIN (p[2 * id-i], mx-i). This is the string card I have been very long. In fact, if you write it a little more complicated, it will be easier to understand:

j = 2 * id-i, that is, J is the symmetric point about ID.
if (Mx-i > P[j])
P[i] = P[j];
else/* P[j] >= mx-i */
P[i] = mx-i; P[i] >= mx-i, take the minimum value, and then match the update.


When Mx-i > P[j], the palindrome string centered on S[j] is contained in a palindrome string centered on S[id], because I and J symmetry, the palindrome substring centered on s[i] is necessarily contained in a palindrome string centered on S[id], so there must be p[i] = p[j].

When P[j] > mx-i, the palindrome string centered on s[j] is not completely contained in a palindrome string centered on S[id], but is based on symmetry, that is, the palindrome string centered on s[i], which extends right at least to the position of MX, i.e. P[i] >= mx I As to whether the post-MX part is symmetrical, it can only be honestly matched.

Since this algorithm is linear in the past, it is backward-swept. Then when we are ready to beg P[i], the p[j before me] we have got. We use the MX in the palindrome before I to extend to the right-most position. At the same time, use the ID variable to note the ID value when the optimal MX is obtained. (Note: In order to prevent character comparisons, I have added another special character ' $ ' before the string that adds ' # '), so the new string subscript is starting from 1.

#include <vector>#include<iostream>using namespacestd;Const intn=300010;intN, P[n];CharS[n], str[n];#define_min (x, y) ((x) < (y)? ( x):(y))voidKP () {inti; intMX =0; intID;  for(I=n; str[i]!=0; i++) Str[i]=0;//There is no problem with this sentence. Can not live ural1297, such as data: Ababa ABA     for(i=1; i<n; i++)    {        if(MX >i) p[i]= _min (p[2*id-i], p[id]+id-i); ElseP[i]=1;  for(; str[i+p[i]] [= Str[i-p[i]]; p[i]++)            ; if(P[i] + i >mx) {mx= P[i] +i; ID=i; }    }}voidinit () {intI, J, K; str[0] ='$'; str[1] ='#';  for(i=0; i<n; i++) {Str[i*2+2] =S[i]; Str[i*2+3] ='#'; } N= N2+2; S[n]=0;}intMain () {inti, ans; while(SCANF ("%s", s)! =EOF) {N=strlen (s);  Init ();  KP (); Ans=0;  for(i=0; i<n; i++)   if(p[i]>ans) ans=P[i]; printf ("%d\n", ans-1); } return 0;}
View Code
//Templates
voidManacher (stringSintLen) {a+='$'; A+='#'; for(intI=0; i<len; i++) {a+=S[i]; A+='#'; } A+= (Char)0; intMX =0, id =0; intL =a.length (); for(inti =0; I < L; i++) {P[i]= mx > I? Min (p[2* Id-i], mx-i):1; while(A[i + p[i]] = = A[i-p[i]]) p[i]++; if(i + p[i] >mx) {mx= i +P[i]; ID=i; }}} is also required in mainintAns =0; for(inti =0; I <2* Len +2; i++) ans= Max (ans, p[i]-1);

1112 human race, longest palindrome common algorithm discussion

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.