hiho#1032: Longest palindrome substring (manacher algorithm o (n) time to find the longest palindrome substring of a string)

Source: Internet
Author: User

#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
75

3

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)

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.