Longest palindrome substring algorithm (string processing problem + various methods to solve) "reprint"

Source: Internet
Author: User

Reprint Address: http://blog.csdn.net/kangroger/article/details/37742639

A palindrome is to read and read backwards, as a result, such as ABCBA or ABBA.

The topic is to get to the longest palindrome substring in a string.

1. Violence Law

The most easy to think of is the violent crack, find out each string, then judge is not a palindrome, found the longest one.

Ask each substring time complexity O (n^2), determine whether the substring is a palindrome O (N), the two are multiplied, so the time complexity is O (n^3).

    String Findlongestpalindrome (String &s)      {          int length=s.size ();//String length          int maxlength=0;//longest palindrome string length          int start;//Longest palindrome string start address for          (int i=0;i<length;i++)//Start address for              (int j=i+1;j<length;j++)//End Address              {                  int tmp1,tmp2;                  for (tmp1=i,tmp2=j;tmp1<tmp2;tmp1++,tmp2--)//judgment is not a palindrome                  {                      if (s.at (TMP1)!=s.at (TMP2)) break                          ;                  }                  if (tmp1>=tmp2&&j-i>maxlength)                  {                      maxlength=j-i+1;                      start=i;                  }              }              if (maxlength>0)                  return s.substr (start,maxlength);//Find substring              return NULL;      }  

2. Dynamic Planning

The substring of the back character string is also a palindrome, such as p[i,j] (indicating that the substring ending with J begins with I) is a palindrome string, then P[i+1,j-1] is also a palindrome string. So the longest palindrome string can be decomposed into a series of sub-problems. This requires additional space O (n^2), and the algorithm complexity is O (n^2).

First, the state equation and the transfer equation are defined:

P[i,j]=0 indicates that the substring [i,j] is not a palindrome string. P[i,j]=1 indicates that the substring [i,j] is a palindrome string.

P[i,i]=1

P[i,j]{=p[i+1,j-1],if (S[i]==s[j])

=0, if (S[i]!=s[j])

    String Findlongestpalindrome (String &s)      {          const int length=s.size ();          int maxlength=0;          int start;          BOOL P[50][50]={false};          for (int i=0;i<length;i++)//initialization preparation          {              p[i][i]=true;              if (i<length-1&&s.at (i) ==s.at (i+1))              {                  p[i][i+1]=true;                  start=i;                  maxlength=2;              }          }          for (int len=3;len<length;len++)//substring length for              (int i=0;i<=length-len;i++)//substring start address              {                  int j=i+len-1 ;//substring End address                  if (p[i+1][j-1]&&s.at (i) ==s.at (j))                  {                      p[i][j]=true;                      Maxlength=len;                      start=i;                  }              }          if (maxlength>=2)              return s.substr (start,maxlength);          return NULL;      }  

3. Center ExpansionThe Central extension is to take each letter of a given string as a center and extend it to both sides to find the longest child palindrome. The algorithm complexity is O (n^2). But there are two things to consider:1, like ABA, so the length is odd. 2, want to ABBA, so the length is even.
    String Findlongestpalindrome (String &s) {const int length=s.size ();          int maxlength=0;                int start;              for (int i=0;i<length;i++)//length is odd {int j=i-1,k=i+1;                  while (j>=0&&k<length&&s.at (j) ==s.at (k)) {if (k-j+1>maxlength)                      {maxlength=k-j+1;                  Start=j;                  } j--;              k++;              }} for (int i=0;i<length;i++)//length is even {int j=i,k=i+1;                  while (j>=0&&k<length&&s.at (j) ==s.at (k)) {if (k-j+1>maxlength)                      {maxlength=k-j+1;                  Start=j;                  } j--;              k++; }} if (maxlength>0) return S.substr (Start,maxlenGTH);      return NULL;   }

4.Manacher method Manacher method can only solve such as ABA, such as an odd length of palindrome string, for ABBA such can not solve, so in the inside to add special characters. I added "#" to make Abba into a#b#b#a. This algorithm is based on the symmetry of the existing palindrome string calculation, the specific algorithm complexity of O (N), I did not see, because there are two nested for loop. The specific principle of reference here. I didn't filter out "#" in the test code.
    #define MIN (x, y) ((x) < (y)? ( x):(y)) #define MAX (x, y) ((x) < (y)? (          Y):(x)) string FindLongestPalindrome3 (string s) {int length=s.size ();              for (int i=0,k=1;i<length-1;i++)//Add # {S.insert (k, "#") to the string;          k=k+2;          } length=length*2-1;//Add # After string length int *rad=new int[length] ();          rad[0]=0; for (int i=1,j=1,k;i<length;i=i+k) {while (i-j>=0&&i+j<length&&s.at (i-j) ==s.              at (I+J)) J + +;              Rad[i]=j-1; for (k=1;k<=rad[i]&&rad[i-k]!=rad[i]-k;k++)//Mirror, Encounter rad[i-k]=rad[i]-k stop, then do not start comparing from J=1 rad[i+k]=min                    (RAD[I-K],RAD[I]-K);          J=max (j-k,0);//Update J} int max=0;          int center;                  for (int i=0;i<length;i++) {if (Rad[i]>max) {max=rad[i];             Center=i; }} return S.substr (center-max,2*max+1);   }

Longest palindrome substring algorithm (string processing problem + various methods to solve) "reprint"

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.