JZOJ2682. WC2012 Singles 12.17 longest double palindrome string

Source: Internet
Author: User
Description

A string called a palindrome that reads exactly the same sequence and reverse order. For example, ACBCA is a palindrome string, and ABC is not (ABC's order is "ABC", Reverse is "CBA", not the same). Enter the string s of length n, to find the longest double palindrome t of S, you can divide t into two parts x, Y, (| x|,| y|≥1) and X and Y are palindrome strings. Input

A line of string s consisting of lowercase English letters. Output

An integer line that represents the length of the longest double palindrome string. Sample Input

Baacaabbacabb Sample Output

Data Constraint

For 10% of data, 2≤| s|≤103.
For 30% of data, 2≤| s|≤104.
For 100% of data, 2≤| s|≤105. Solving

The topic requires cutting from one position,
Then both sides are palindrome strings.

So just enumerate which position is cut.
First use Manacher to find the longest palindrome radius with each position as a palindrome center.
And then, for each point, the left and right can reach its farthest palindrome center.
Then enumerate the cut positions,
To avoid a letter being divided into two palindrome strings at the same time, the cut position can only be added to the character,
Use the location of the farthest Palindrome center on the left and right, to find out the answer. Code

#include <queue> #include <cstdio> #include <iostream> #include <algorithm> #include < cstring> #include <string.h> #include <cmath> #include <math.h> #define LL Long long #define ULL Unsi
gned Long Long #define N 100008 #define M 102 #define DB double #define P putchar #define G getchar #define MO 1000000007
#define ZM 19260817 using namespace std;
Char ch;
    void read (int &n) {n=0;
    Ch=g ();
    while ((ch< ' 0 ' | | ch> ' 9 ') && ch!= '-') ch=g ();
    ll W=1;
    if (ch== '-') w=-1,ch=g ();
    while (' 0 ' <=ch && ch<= ' 9 ') n= (n<<3) + (n<<1) +ch-' 0 ', ch=g ();
N*=w; } DB Max (db a,db b) {return a>b?a:b;} int min (int a,int b) {return a<b?a:b;} ll ABS (LL x) {return x<0?-x:x;} ll Sqr (ll X)
{return x*x;} void Write (ll x) {if (x>9) write (X/10);

P (x%10+ ' 0 ');}
Char s[n*2],t[n*2];

int len,ans,p[n*2],l[n*2],r[n*2],mx,id;
    void Manacher () {mx=0; for (int i=1;i<=len;i++) {if (mx>=i) p[i]=min (mx-i,p[id*2-i]); else p[i]=0;
        while (s[i-p[i]-1]==s[i+p[i]+1]) p[i]++;
    if (Mx<i+p[i]) mx=p[i]+i,id=i;
    }} int main () {scanf ("%s", t+1);
    Len=strlen (t+1);
    s[0]= ' # ';
    for (int i=1;i<=len;i++) s[(i<<1) -1]= ' * ',s[i<<1]=t[i];
    s[(len<<1) +1]= ' * ';
    Len= (len<<1) +1;
    s[len+1]= '! ';

    Manacher ();
    mx=0; for (int i=1;i<=len && mx<len;i++) if (I+P[I]&GT;MX) {for (int j=mx+1;j<=i+p[i]
            ; j + +) L[j]=i;
        Mx=i+p[i];
    } mx=len+1;
                for (int i=len;i && mx;i--) if (I-P[I]&LT;MX) {for (int j=i-p[i];j<mx;j++)
            R[j]=i;
        Mx=i-p[i];
    } for (int i=1;i<=len;i++) if (s[i]== ' * ') Ans=max (ans, (((i-l[i)) <<1) + ((r[i]-i) <<1) +1) >>1);
Write (ANS); }

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.