Palindrome String---palindrome

Source: Internet
Author: User

POJ 3974

Description

Andy the smart Computer Science student is attending a algorithms class when the professor asked the students a simple q Uestion, "Can propose an efficient algorithm to find the length of the largest palindrome in a string?"

A string is said to being a palindrome if it reads the same both forwards and backwards, for example ' Madam ' is a palindrome While the "ACM" is not.

The students recognized that's a classical problem but couldn ' t come up with a solution better than iterating over a ll substrings and checking whether they be palindrome or not, obviously this algorithm was not efficient at all, after a W Hile Andy raised his hand and said "Okay, I ' ve a better algorithm" and before he starts to explain his idea he stopped for A moment and then said "Well, I ' ve an even better algorithm!".

If you think know Andy's final solution then prove it! Given a string of 1000000 characters find and print the length of the largest palindrome inside this string.

Input

Your program would be tested on at the most of the test cases, each test case is given as a string of in most 1000000 lowercase cha Racters on a line by itself. The input is terminated by a line, starts with the string "END" (quotes for clarity).

Output

For each test case in the input print the test case number and the length of the largest palindrome.

Sample Input

Abcbabcbabcbaabacacbaaaabend

Sample Output

Case 1:13case 2:6

#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespacestd;Const intn=2000010;intN,p[n];CharS[n],str[n];voidKP () {inti; intmx=0; intID; ///For (i=n;str[i]!=0;i++)///str[i]=0;///without this sentence has a problem, can not live ural1297, such as data: Ababa ABA;     for(i=1; i<n;i++)    {        if(mx>i) p[i]=min (p[2*id-i],p[id]+id-H); ElseP[i]=1;  for(; Str[i+p[i]]==str[i-p[i]];p [i]++); if(p[i]+i>mx) {mx=p[i]+i; ID=i; }    }}voidinit () {str[0]='$'; str[1]='#';  for(intI=0; i<n;i++) {Str[i*2+2]=S[i]; Str[i*2+3]='#'; } N=n*2+2; S[n]=0;}intMain () {intCase=1;  while(SCANF ("%s", s)! =EOF) {        if(s[0]=='E'&&s[1]=='N'&&s[2]=='D')             Break; N=strlen (s);        Init ();        KP (); intans=0;  for(intI=1; i<n;i++)            if(p[i]>ans) ans=P[i]; printf ("Case %d:%d\n", case++,ans-1); }    return 0;}

Palindrome String---palindrome

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.