Palindrome String---Longest palindrome

Source: Internet
Author: User

HDU 3068

Description

A string s consisting of a lowercase English character a,b,c...y,z is given, and the length of the longest palindrome in S is obtained.
Palindrome is the same as the inverse of the string, such as ABA, ABBA, etc.

Input

Input has multiple sets of case, no more than 120 groups, each set of input is a line of lowercase English characters a,b,c...y,z string s
Between two sets of case separated by a blank line (the empty line is not processed)
String length len <= 110000

Output

Each row is an integer x, corresponding to a set of case, representing the longest palindrome length contained in the string for the group case.

Sample Input

Aaaaabab

Sample Output

43 string to find the longest palindrome template title: The code is as follows:
#include <iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespacestd;Const intn=120005;intp[2*N];Chars[2*n],str[2*N];intN;voidKP () {intmx=0; intID; ///For (i=n;str[i]!=0;i++)///str[i]=0;///without this sentence has a problem, can not live ural1297, such as data: Ababa ABA;     for(intI=1; i<n;i++)    {        if(mx>i) p[i]=min (p[2*id-i],p[id]+id-i); ElseP[i]=1;  for(; Str[i+p[i]]==str[i-p[i]];p [i]++); if(p[i]+i>mx) {mx=p[i]+i; ID=i; }    }}voidinit () {str[0]='$'; str[1]='#';  for(intI=0; i<n;i++) {Str[i*2+2]=S[i]; Str[i*2+3]='#'; } N=n*2+2; S[n]=0;}intMain () { while(SCANF ("%s", &s)! =EOF) {N=strlen (s);        Init ();        KP (); inttmp=0;  for(intI=1; i<n;i++)            if(p[i]>tmp) tmp=P[i]; printf ("%d\n", tmp-1); }    return 0;}

Palindrome String---Longest palindrome

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.