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