Manacher algorithm for finding the longest palindrome string

Source: Internet
Author: User

The longest palindrome substring has been used in the suffix array, today saw a good article to write O (n) to find the longest palindrome, so it's a note

http://zhuhongcheng.wordpress.com/2009/08/02/ a-simple-linear-time-algorithm-for-finding-longest-palindrome-sub-string/in English

http://acm.uestc.edu.cn/bbs/read.php?tid=3258 UESTC BBS

Palindrome string including odd-length and even-length, the general demand for the time to discuss the situation, the algorithm has done a simple processing to unify the parity situation. The basic idea of the algorithm is this, the original string each character in the middle with a string of characters not appearing in the character (uniform parity), with an array p[i] record the str[I] as the middle character of the palindrome string to the right to match the length. Let's look at an example.

Original string: W a A B w s W f D

New string: # W # a # a # b # w # s # w # f # d #

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

P Array: 1 2 1 2 3 2 1 2 1 2 1 4 1 2 1 2 1 2 1

By the nature of the P array, the new string with Str[i] as the middle character of the palindrome string length is p[i]-1, with # as the middle character is the length is even, with the non-# is the middle character is the length is odd, then how to find p[] array it.

From left to right calculation, that is, calculate p[i] p[0.....i-1] All to calculate, and with a variable MX record max{k+p[K]} (K=0.....i-1), with the ID of the admission maximum value of K, then p[i]= min (p[2*id-i], Mx-i)

For the first picture, the palindrome string with I as the intermediate character is covered by a palindrome string with the ID intermediate character, by symmetry, p[i] = p[2*id-i]. For the second picture is not completely overwritten, so for the k>mx character, a match is required to determine p [i].

Realize:

STR is the string after the insertion of characters, adding a word such as ' ¥ ' at the top of the string, to prevent the comparison when the pointer is less than 0
//So the actual str is a
void Calp (int n)
{
	int i,mx=0,id
	starting from subscript 1; for (i=1;i<=n;i++)
	{
		if (mx>i)
			p[i]=min (p[2*di-i],mx-i);
		else
			p[i]=1;
		for (; Str[i+p[i]]==str[i-p[i]];p [i]++)  //not completely covered
			;
		if (p[i]+i>mx)
		{
			mx=p[i]+i;
			id=i;}}}


	

Analysis of algorithm complexity, because the maximum number of characters n times, so the complexity of O (n)

May ask, here is asked what choose K+p[k] the largest corresponding ID, the others do not, in fact, the other is also OK, choose this is to reduce the number of comparisons as large as possible, because in the end you still want to

for (; Str[i+p[i]]==str[i-p[i]];p [i]++)  //not completely covered
			;
Run this statement.

I have limited ability to express, please see the original link

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.