Longest palindromic sequence (Manacher Algorithm)

http://acm.hdu.edu.cn/showproblem.php?pid=3068

The longest palindrome

Problem Description
A string S is given only by the lowercase English characters a, b, c ... y, z consisting of seeking the longest length S palindromic strings.
Palindrome is reverse read character string is the same as aba, abba, etc.
 

 

Input
Multiple sets of input case, not more than 120 groups of lower-case characters input line a, the string S b, c ... y, z component
between the two groups separated by an empty line case (not the blank line processing)
string length len <= 110000
 

 

Output
Each row of an integer x, corresponding to a set of case, the length of the string represents the longest palindromic group contained in the case.
 

 

Sample Input
aaaa abab
 

 

Sample Output
4 3
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:   1358  1686  3336  3065  3746
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
using namespace std;
const int N = 500009 ;
int c[N] ;
int n ;
220009p [int];
 Char A [ 110009 ];
 char B [ 220 009 ]; 


int main () 
{ 

    the while (~ Scanf ( " % S " , A)) 
    { 
        int Lena = strlen (A);
         // string processed by extraneous characters increases, the number of changes even palindromic sequence 
        B [ 0 ] = ' $ ' ; 
        B [ . 1 ] = ' # ' ;
         int L = 2 ;
         for ( int I = 0 ; I <Lena; I ++) 
        { 
            B [L ++] = A [I]; 
            B [L ++] = ' # ' ; 
        } 
        B [L] = ' \ 0 ' ;
         int MX = - . 1 ;    // rightmost right border palindromic : it refers to a palindrome position and before the sub-location of the string, reaches the rightmost place of the 
        int len = - . 1 ;
         int mid; // to mid-centered longest palindromic sequence 
        for ( int I = 0 ; I <L; I ++ ) 
        { 
            // a common four cases 
            
            IF (I <MX) //If the string is the longest palindromic left before the position 
            {
                 // following expression contains three cases
                 @ 2 MID * - i. 1 is the symmetry point
                 // as the point of symmetry traversed
                 // can be based on the point i determining the length of the longest string palindrome
                 // wherein two cases can be derived palindromic i longest run length time complexity is o (1) directly from the symmetrical points;
                 // there is a need to r Based on the two sides to continue inquiries. 
                P [I] = min (P [ 2 * MID - I], MX - I); 
            } 
            
            the else // If it is a traverse to the right border a 
            { 
                P [I] = . 1 ; // P character array is recorded palindromic each character length of the longest radius palindromic string string 
            }
             the while(B [I - P [I]] == B [I + P [I]]) // to both sides of a query 
                P [I] ++ ;
             IF (MX <P [I] + I) 
            { 
                MID = I; 
                MX = P [I] + I; 
            } 
            len = max (len, P [I] - . 1 ); // record the longest string palindromic entire string 
        } 
        COUT << len << endl; 
    } 


    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/nonames/p/11282901.html