#1032: Maximum palindrome substring time limit:1000msSingle Point time limit:1000msMemory 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?
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. "
Hint a hint two hint three hint four
-
-
Sample input
-
-
3abababaaaaabaaacacdas
-
-
Sample output
-
-
753
-
-
Looked all morning like I only understand the brute force of the solution to some clever algorithm but it is not clear that only slowly learning alas
-
-
Algorithm for processing the longest palindrome string manacher in the longest palindrome string of the Manacher's Algorithm:o (n) time to understand a little bit about understanding the topic AC First
-
-
#include <stdio.h> #include <algorithm> #include <string.h>using namespace Std;char str1[1000005]; Char str[1000005*2];int p[1000005*2];int manacher () {int id=0,mx=0;int max=1;for (int i=0;str[i]!= ' n '; i++) {p[i]=mx >i?min (p[2*id-i],mx-i): 1;while (Str[i+p[i]]==str[i-p[i]]) p[i]++;if (i+p[i]>mx) {mx=i+p[i];id=i;} if (P[i]>max) max=p[i];} return max-1;} int main () {int ncase;scanf ("%d", &ncase), while (ncase--) {memset (str1,0,sizeof (str1)); memset (str,0,sizeof (str)) ; memset (P,0,sizeof (P)); scanf ("%s", str1); int j=2; Str[0]= ' _ ';//Avoid crossing str[1]= ' @ '; for (int i=0;str1[i]!= ' + '; i++) {str[j++]=str1[i];str[j++]= ' @ ';} Str[j]= ';p rintf ("%d\n", Manacher ());} return 0;}
hiho#1032: Longest palindrome substring (manacher algorithm o (n) time to find the longest palindrome substring of a string)