Portal
#1032: Maximum palindrome substring time limit:1000msSingle Point time limit:1000msMemory Limit:64MBDescribe
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?
Little hi smiled and said: "This is very easy, you just need to write a program, first read an integer N(n<=30)from the standard input, representing the number of strings I gave you, and then the next is the N string I want to give you (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. "
Tip OneTip Two tips for Three tips four
-
Sample input
-
3abababaaaaabaaacacdas
-
Sample output
-
753
Exercises
Manacher algorithm--o (n) palindrome substring algorithm
Go straight to a Daniel's train of thought and speak very well:
http://blog.csdn.net/ggggiqnypgjg/article/details/6645824/
http://acm.hust.edu.cn/vjudge/problem/viewSource.action?id=140283
Results:Accepted Submission Time:2015-05-07 14:11:06
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <stack>6#include <cctype>7#include <vector>8#include <cmath>9 Ten #definell Long Long One A using namespacestd; - - Const intM = +; the Const intN =1000010; - Constll mod =1000000007; - - intT; + CharTe[n]; - Chars[2*N]; + intp[2*N]; A intans; at intl; - - voidPre ()//to prevent character comparisons from crossing, I added another special character ' $ ' before the string that added ' # ', so the new string subscript is starting from 1 - { - intl1=strlen (TE); -L=2*l1+2; ins[0]='$'; -s[1]='#'; to for(intI=0; i<l1;i++){ +s[i*2+2]=Te[i]; -s[i*2+3]='#'; the } *s[l]=' /'; $ }Panax Notoginseng - voidManacher () the { + intI,mx,id;//we use the MX in the palindrome before I to extend to the right-most position. At the same time, use the ID variable to note the ID value when the optimal MX is obtained. Amx=0; the for(i=1; i<l;i++){ + if(mx>i) { -P[i]=min (p[2*id-i],mx-i); $ } $ Else{ -p[i]=1; - } the for(; s[i+p[i]]==s[i-p[i];p [i]++){ - if(i+p[i]>MX) {Wuyimx=i+P[i]; theId=i; - } Wu } - } About } $ - intMain () - { - //freopen ("data.in", "R", stdin); Ascanf"%d",&T); + for(intCcnt=1; ccnt<=t;ccnt++){ the //while (scanf ("%d%d", &a,&b)! = EOF) { -scanf"%s", TE); $ pre (); the Manacher (); the inti; theans=1; the for(i=1; i<l;i++){ -Ans=max (ans,p[i]-1); in } theprintf"%d\n", ans); the } About return 0; the}
Hihocoder #1032: Longest palindrome substring [manacher algorithm--o (n) palindrome substring algorithm]