Manacher algorithm to find the longest palindrome subsequence

Source: Internet
Author: User

One: Background

Given a string, the longest palindrome substring is obtained. For example:

      1. S= "ABCD", the longest palindrome length is 1;
      2. S= "Ababa", the longest palindrome length is 5;
      3. S= "ABCCB", the longest palindrome length is 4, that is, BCCB.

The traditional idea of the above question is to iterate through each character, looking at the character as the center of the two sides. Its time complexity is O (n^2), the efficiency is very poor.

1975, a man named Manacher invented an algorithm, the Manacher algorithm (Chinese name: horse-drawn car algorithm), the algorithm can increase the complexity of the time to O (n). Let's look at how the horse-drawn car algorithm works.

Two: Algorithm process analysis

Since palindrome is divided into even palindrome (such as BCCB) and odd palindrome (such as BCACB), and in dealing with odd and even problems will be more cumbersome, so here we use a technique, the practice is: in the string, and each character between the insertion of a character (if the character does not appear in the string).

For example: s="abbahopxpo" , convert to s_new="$#a#b#b#a#h#o#p#x#p#o#" (here the character $ just to prevent out of bounds, the following code will be explained), so, s in the beginning there is a palindrome abba and a singular palindrome opxpo , converted to #a#b#b#a# and, the #o#p#x#p#o# length is converted to an odd number.

Defines a secondary array that represents the radius of the longest int p[] p[i] palindrome centered on I, for example:

I 0 1 2 3 4 5 6 7 8 9 Ten One A - - the - - - +
S_new[i] $ # A # B # B # A # H # O # P # X # P #
P[i] 1 2 1 2 5 2 1 2 1 2 1 2 1 2 1 4 1 2 1

As you can see, p[i] - 1 it is exactly the length of the longest palindrome in the original string.

The next point is to solve the P array, such as:

Set two variables, MX and ID. MX represents the right boundary of the longest palindrome with ID as the center, that is mx = id + p[id] .

Suppose we now seek, that is, the longest p[i] palindrome radius centered on I, if i < mx , for example, then:

if (I < mx)       = Min (p[2 * id-i], mx-i);

2 * id - iAs I about the symmetric point of the ID, that is, the J Point, which represents the longest p[j] palindrome radius centered on J , so we can use it p[j] to speed up the lookup.

Three: Code
//Specifies the position of the palindrome, which is titled Specifying the longest Palindrome sequence containing the last one. Charma[maxn*2], S[MAXN];intmp[maxn*2];intAns,mlen;voidManacher (CharS[],intLen) {    intL=0; Ma[l++]='$'; Ma[l++]='#';  for(intI=0; i<len; i++) {ma[l++]=S[i]; Ma[l++]='#'; } Ma[l]=0; intmx=0, id=0;  for(intI=0; i<l; i++) {Mp[i]=mx>i?min (mp[2*id-i],mx-i):1;  while(ma[i+mp[i]]==ma[i-Mp[i]]) Mp[i]++; if(i+mp[i]>mx) {mx=i+Mp[i]; ID=i; }//Here you can check (Ma[i])Ans=max (ans,mp[i]-1); if(mp[i]-1+i==l-1) Mlen=max (mlen,mp[i]-1); }}intMain () {intT; CIN>>u; intKcase =1;  while(t--) {memset (MA,0,sizeof(MA)); Memset (MP,0,sizeof(MP)); CIN>>s; intlen=strlen (s); Ans=0; Mlen=0;        Manacher (S,len); if(ans = =len) printf ("Case %d:%d\n", kcase++, ans); Elseprintf ("Case %d:%d\n", kcase++, Len-mlen +Len); }}

Four: Topics

The topic is to add a judging condition on the original basis and see where to add it.

//101350i-2017 ACM Arabella Collegiate Programming contest-mirrored String II#include <bits/stdc++.h>using namespaceStd;typedefLong Longll;Const intmaxn=1010;Charma[maxn*2];intmp[maxn*2];CharS[MAXN];intCheckCharzzz) {    if(zzz=='A'|| zzz=='H'|| zzz=='I'|| zzz=='M'|| zzz=='O'|| zzz=='#'||zzz=='T'|| zzz=='U'|| zzz=='V'|| zzz=='W'|| zzz=='X'|| zzz=='Y')        return 1; return 0;}voidManacher (CharS[],intLen) {    intL=0; Ma[l++]='$'; Ma[l++]='#';  for(intI=0; i<len; i++) {ma[l++]=S[i]; Ma[l++]='#'; } Ma[l]=0; intmx=0, id=0;  for(intI=0; i<l; i++) {Mp[i]=mx>i?min (mp[2*id-i],mx-i):1;  while(check (Ma[i+mp[i]]) &&ma[i+mp[i]]==ma[i-mp[i])//Add a check here{Mp[i]++; }        if(i+mp[i]>mx) {mx=i+Mp[i]; ID=i; }    }}intMain () {intT; CIN>>T;  while(t--) {cin>>s; intlen=strlen (s);        Manacher (S,len); intans=0;  for(intI=0; i<len*2+2; i++)            if(check (Ma[i]))//Add check hereAns=max (ans,mp[i]-1); cout<<ans<<Endl; }}

Manacher algorithm to find the longest palindrome subsequence

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.