51nod1089 longest palindrome substring V2 (manacher algorithm)

Source: Internet
Author: User
Tags min time limit

1089 Maximum palindrome substring V2 (manacher algorithm) base time limit: 1 seconds space limit: 131072 KB score: 0 Difficulty: Basic problem collection concern Palindrome string refers to Aba, ABBA, CCCBCCC, AAAA, the symmetry of the left and right strings. Enter a string str to output the length of the longest palindrome substring in str. Input

Input str (str length <= 100000)
Output
Outputs the length of the longest palindrome substring L.
Input example
Daabaac
Output example
5


#include <stdio.h>
#include <string.h>
char s[200010];int p[200010];
int min (int a,int b)
{
	return a<b?a:b;
}
void f ()
{
	int l=strlen (s);
	for (int i=l;0<i;i--)
	{
		s[i*2+1]= ' # ';
		S[I*2]=S[I-1];
	}
	S[0]= ' * ', s[1]= ' # ';
}
int Manacher ()
{
	f ();
	int  mx = 0, id = 0,max=1;
	memset (p, 0, sizeof (p));
	for (int i = 1; s[i]! = i++) 
	{
	    p[i] = mx > I min (p[2 * id-i], mx-i): 1;
	    while (S[i + p[i]] = = S[i-p[i]]) 
	        p[i]++;
	    if (i + p[i] > mx) 
	    {
	        mx = i + p[i];
	        id = i;
	    }
	    max=max>p[i]?max:p[i];
	}
	return max-1;
}
int main ()
{
	int l,i;
	while (scanf ("%s", s)!=eof)
		printf ("%d\n", Manacher ());
	return 0;
}


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.