[Hihocoder] #1032: Longest palindrome string

Source: Internet
Author: User

Time limit: 1000ms single point time limit: 1000ms memory limit: 64MB description

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

The legendary Manacher algorithm, the core of the algorithm is in this sentence: p[i] = min (P[2*id-i], P[id] + id-i);

Original address:
http://zhuhongcheng.wordpress.com/2009/08/02/a-simple-linear-time-algorithm-for-finding-longest-palindrome-sub-string/
In fact, the original word is relatively clear, just English, I write a Chinese here.
First: We all know what is called palindrome string, this algorithm to solve is a string of the longest palindrome substring how long. This algorithm can find out the length of the longest palindrome with each character centered in the time complexity of O (n), in the case of linear time complexity.
This algorithm has a very ingenious place, it takes the odd palindrome string and even the palindrome string unified together to consider. This has always been a problem in the issue of palindrome is more annoying place. The algorithm also has a good place is to take full advantage of the character matching the particularity, avoid a large number of unnecessary duplicate matching.
The approximate process of the algorithm is this. Insert a delimiter in the middle of each of the two adjacent characters first, but this delimiter is not present in the original string. Can usually be separated by ' # '. This is very ingenious to the odd-length palindrome string and even-length palindrome unified to consider (see below, the palindrome string length is all odd), and then use an auxiliary array p to record each character as the center of the longest palindrome string information. P[id] records the longest palindrome string centered on the character Str[id], when Str[id] is the first character, the longest palindrome extends to the right P[id] characters.
Original string: W AA bwsw F D
New string: # W# A # a #B# W # s # w #F # D #
Auxiliary array p:1 2 1 2 3 2 1 2 1 2 1 4 1 2 1 2 1 2 1
Here is a good property, P[id]-1 is the length of the palindrome string in the original string (including ' # '). If this is not particularly clear, you can take out the paper to draw a picture, you experience. Of course, everyone here may be different, but I think the general idea should be the same.
OK, let's go on. The key question now is how to find the P array within the O (n) time complexity. As long as the P array is calculated, the longest palindrome string can be directly swept through it.
Since this algorithm is linear in the past, it is backward-swept. Then when we are ready to beg P[i], the p[j before me] we have got. 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. (Note: In order to prevent character comparisons, I have added another special character ' $ ' before the string that adds ' # '), so the new string subscript is starting from 1.

if(mx > i) p[i]= MIN(P[2*id-i], mx-i); Is the furthest length of the current face comparison Mx>i, P[i] has a minimum value.   The core idea of this algorithm is here, why does the P array satisfy such a property? (The following section is in the form of a picture)


1#include <bits/stdc++.h>2 using namespacestd;3 4 intN;5 strings;6 7 voidsolve () {8     stringS1;9S1.resize (2* S.size () +2);Tens1[0] ='$'; Ones1[1] ='#'; A      for(inti =0; I < s.size (); ++i) { -s1[(i +1) <<1] =S[i]; -s1[((i +1) <<1) +1] ='#'; the     } -vector<int> P (s1.size (),0); -     intres =0; -      for(intID =0, i =1; I < s1.size (); ++i) { +         if(P[id] + ID > i) p[i] = min (p[2* Id-i], P[id] + ID-i); -         ElseP[i] =1; +          while(S1[i + p[i]] = = S1[i-p[i]] + +P[i]; A         if(i + p[i] > ID + p[id]) id =i; atres =Max (res, p[i]); -     } -cout << Res-1<<Endl; - } -  - intMain () { inCIN >>N; -      while(n--) { toCIN >>s; + solve (); -     } the     return 0; *}

[Hihocoder] #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.