#1032: Longest palindrome substring (manacher algorithm)

Source: Internet
Author: User


This blog post is good for manacher.

#1032: Longest palindrome string
Time limit: 1000ms
Single point time limit: 1000ms
Memory Limit: 64MB

Describe

Small hi and small ho is a pair of good friends, born in the information society, they have a great interest in programming, they agreed to help each other, in the programming of learning along the road together.

On this day, they encountered a string of strings, so little hi to small ho raised the classic question: "Little ho, you can find each of them in these strings each of the longest palindrome string it?" ”

Little Ho asked curiously, "What is the longest palindrome string?" ”

Little hi replied: "A continuous paragraph in a string is the substring of this string, and palindrome string refers to the 12421 of this from the back to read and read from the back to the same string, so the longest palindrome substring is the longest string of a palindrome is the substring ~"

Little Ho Way: "That's it!" So how do I get these strings? What should I tell you about the longest palindrome string I've calculated?

Small hi smiled and said: "This is very easy, you just need to write a program, first read from the standard input an integer N (n<=30), the number of strings I gave you, and then the next is I will give you the N string (string length <=10^6). And you have to tell me your answer, as long as you calculate the length of the longest palindrome string in the order I give you to output to the standard output on it! You see, this is an example. ”
Hint a hint two hint three hint four
Sample input

3
Abababa
Aaaabaa
Acacdas

Sample output

7
5

3

#include <bits/stdc++.h>using namespace Std;template<class t>inline T Read (t&x) {char C;    while ((C=getchar ()) <=32) if (c==eof) return 0;    BOOL Ok=false;    if (c== '-') Ok=true,c=getchar ();    for (x=0; c>32; C=getchar ()) x=x*10+c-' 0 ';    if (OK) x=-x; return 1;} Template<class t> inline T read_ (t&x,t&y) {return read (x) &&read (y);} Template<class t> inline T read__ (t&x,t&y,t&z) {return read (x) &&read (y) &&read (z);}    Template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x;    if (x<10) putchar (x+ ' 0 '); else write (X/10), Putchar (x%10+ ' 0 ');}    Template<class t>inline void Writeln (T x) {write (x); Putchar (' \ n ');} -------ZCC IO template------const int MAXN=1E6+1000;CONST double inf=999999999; #define Lson (rt<<1), L,m#define Rson (rt<<1|1), M+1,r#define M ((l+r) >>1) #define for (i,t,n) for (int i= (t);i< (n); i++) typedef long Long LL; typedef double DB;TYPEDEF PAir<int,int> P; #define BUG printf ("---\ n"); #define MOD 100000000char S[maxn];char a[maxn*2];int d[maxn*2];int Init    () {int N,len=strlen (s);    A[0]= ' $ ';    a[1]= ' # ';        for (int i=0;i<len;i++) {a[i*2+2]=s[i];    a[i*2+3]= ' # ';    } n=len*2+2;    a[n]=0; return n;}    void Manacher (int len) {int pos=0,mx=0;        for (int i=1;i<len;i++) {if (mx>i) d[i]=min (d[pos*2-i],mx-i);        else d[i]=1;        while (A[i+d[i]]==a[i-d[i]]) d[i]++;            if (d[i]+i>mx) {mx=d[i]+i;        Pos=i;    }}}int Main () {int t,n;    Read (T);        while (t--) {scanf ("%s", s); N=init ();        Manacher (n);    Writeln (*max_element (d,d+n)-1); } return 0;}


#1032: Longest palindrome substring (manacher algorithm)

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.