#1032: Longest palindrome string

Source: Internet
Author: User

Time limit: 1000ms
Single point time limit: 1000ms
Memory 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?

Small hi smiled and said: "This is very easy, you just need to write a program, first read from the standard input an integer N (n<=30), the number of strings I gave you, and then the next is I will give you the N string (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
3
Abababa
Aaaabaa
Acacdas
Sample output
7
5
3
Key to this topic:
1, is to use manacher algorithm to solve the longest palindrome string, the time complexity of O (N).
2, this topic can reach 1000000 when the data is maximum. So in order to avoid wasting a lot of time in constructing new strings, we pre-allocate memory to arrays.

#include <iostream>#include <string>#include <algorithm>using namespace STD;Static stringAFTERPROCESSSTR (1000002*2,' # ');//Pre-allocate memory and initializeintPreprocess (string&AMP;STR)//Pre-processing time as long as the odd position of the ' # ' to replace the good! {//string afterprocessstr= "#";     for(intI=0; I<str.size (); ++i) {Afterprocessstr [2*i+1]= Str[i]; }returnStr.size () *2+1;//afterprocessstr.clear ();}intMaxpalindrome (string&AMP;STR) {intSizestr=preprocess (str);//cout<<afterprocessstr<<endl;   intMaxedge=0, center=0;int*p=New int[Sizestr] ();intans=0; for(intI=1; i<sizestr;++i) {p[i]= (maxedge>i) min (maxedge-i,p[2*center-i]):0; while(I-1-p[i]>=0&&i+1+p[i]<sizestr&&afterprocessstr[i+1+p[i]]==afterprocessstr[i-1-p[i]] ++p[i];if(I+p[i]>maxedge)              {center=i;          Maxedge=i+p[i]; }if(P[i]>ans) ans=p[i]; }returnAns;}intMain () {intTnCin>>tn;stringStr Str.reserve (1000002);intans,tmp; for(intYe=0; ye<tn;++ye) {Cin>>str;cout<<maxpalindrome (str) <<endl; }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

#1032: Longest palindrome 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.