Longest palindrome substring

Source: Internet
Author: User

I. Cited examples

#1032: Longest palindrome string


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. ”

second, Manacher algorithm

First, in a very ingenious way, all possible odd/even length palindrome strings are converted to odd lengths: a special symbol is inserted on both sides of each character. For example, Abba becomes #a #b#b#a#, ABA becomes #a #b#a#. To further reduce the complexity of the encoding, you can add another special character at the beginning of the string, so that you do not have to deal with cross-border issues specifically, such as $ #a #b#a#.

The following is an example of string 12212321, which, after the previous step, became s[] = "$ #1 #2#2#1#2#3#2#1#";

Then use an array of p[i] to record the length of the longest palindrome string centered on the character S[i] to the left/right (including s[i], that is, the length of the palindrome string "fold", such as the corresponding relationship between S and P:

S # 1 # 2 # 2 # 1 # 2 # 3 # 2 # 1 #
P 1 2 1 2 5 2 1 4 1 2 1 6 1 2 1 2 1


P[3]=2 is also exactly the total length of the palindrome string in the original string s, which is centered at 2, that is, the palindrome string 122 has a length of 3.
So we just need to ask for the array p, then the maximum value in P is the length of the longest palindrome in S.

So how do you calculate P[i]? The algorithm adds two helper variables (in fact one is enough, two clearer) ID and MX, where the ID represents the location of the largest palindrome substring center, and MX is id+p[id], which is the boundary of the largest palindrome substring.

Then you can get a very magical conclusion, the key point of this algorithm is here: if mx > I, then p[i] >= MIN (p[2 * id-i], mx-i). This is the string card I have been very long. In fact, if you write it a little more complicated, it will be easier to understand:

j = 2 * id-i, that is, J is the symmetric point about ID.
if (Mx-i > P[j])
P[i] = p[j];
else/* P[j] >= mx-i */
P[i] = mx-i;//p[i] >= mx-i, take the minimum value, and then match the update.

Use [figure to understand]

third, the test code is as follows:

1 /*2 **************************** Longest palindrome string (mancher) *************************************3 ******************************by ja/c++ 2015-1-16****************************************4 */5 6#include <iostream>7#include <string>8#include <algorithm>9 Ten using namespacestd; One  A stringPreprocess (strings) { -     intn =s.length (); -     if(n = =0)return "^$"; the     stringRET ="^"; -      for(inti =0; I < n; i++) -RET + ="#"+ S.SUBSTR (i,1); -  +RET + ="#$"; -     returnret; + } A  at intLongestpalindrome (strings) { -     stringT =preprocess (s); -     intn =t.length (); -     int*p =New int[n]; -     intC =0, R =0; -      for(inti =1; I < n-1; i++) { in         intI_mirror =2* C-I;//equals to I ' = C-(i-c) -  toP[i] = (R > i)? Min (r-i, P[i_mirror]):0; +  -         //attempt to expand palindrome centered at I the          while(T[i +1+ P[i]] = = T[i-1-P[i]]) *p[i]++; $ Panax Notoginseng         //If palindrome centered at I expand past R, -         //Adjust center based on expanded palindrome. the         if(i + p[i] >R) { +C =i; AR = i +P[i]; the         } +     } -  $     //Find The maximum element in P. $     intMaxLen =0; -     //int centerindex = 0; -      for(inti =1; I < n-1; i++) { the         if(P[i] >maxlen) { -MaxLen =P[i];Wuyi             //centerindex = i; the         } -     } Wu delete[] P; -  About     //return S.substr ((CenterIndex-1-MaxLen)/2, maxlen); $     returnMaxLen; - } -  - intMain () A { +     intN; the     strings; -CIN >>N; $      while(n--) the     { theCIN >>s; thecout << Longestpalindrome (s) <<Endl; the     } -}
View Code


Reference documents:

1.Beeder's Blog "longest palindrome string"

2.LeetCode "Longest palindromic Substring"

3.DDYYXX Programmer's Road "Manacher algorithm Summary"

Longest palindrome substring

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.