poj3974 palindrome "palindrome" "Hash"

Source: Internet
Author: User

Palindrome
Time Limit: 15000MS Memory Limit: 65536K
Total Submissions: 13157 Accepted: 5028

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

Source

Seventh ACM Egyptian national programming Contest

Test instructions: To find the longest palindrome substring of a string

Idea: Palindrome string is actually a node for the middle, the strings are the same at both ends. The same hash function of the previous comparison string is in order from left to right, then this saves a right-to-left string hash value. For each character, the length of the second left Antimeron string, the length of the palindrome string is odd or even two cases.

1#include <iostream>2#include <Set>3#include <cmath>4#include <stdio.h>5#include <cstring>6#include <algorithm>7#include <map>8 using namespacestd;9typedefLong LongLL;Ten #defineINF 0x7f7f7f7f One  A Const intMAXN = 1e6 +5; - CharS[MAXN]; -UnsignedLong LongH[MAXN], P[MAXN], H_REV[MAXN]; the  -UnsignedLong LongGeth (intIintj) - { -     returnH[J]-H[i-1] * p[j-i +1]; + } -  +UnsignedLong LongGethrev (intIintj) A { at     returnH_rev[i]-h_rev[j +1] * p[j-i +1]; - } -  - intMain () - { -     intCAS =1; inp[0] =1; -      for(inti =1; i < MAXN; i++){ toP[i] = p[i-1] *131; +     } -      while(SCANF ("%s", S +1)){ the         if(strcmp (S +1,"END") ==0){ *              Break; $         }Panax Notoginseng         intn = strlen (s +1); -h[0] =0; theH_rev[n +1] =0; +          for(inti =1; I <= N; i++){ AH[i] = h[i-1] *131+ (S[i]-'a'+1); the         } +          for(inti = n; I >=1; i--){ -H_rev[i] = h_rev[i +1] *131+ (S[i]-'a'+1); $         } $  -         intAns =-1; -          for(inti =1; I <= N; i++){ the             intped = min (i-1, n-i), PST =1; -              while(PST <PED) {Wuyi                 intPMID = (PST + PED +1) /2; the                 //cout<<pmid<<endl; -                 if(Geth (I-pmid, I-1) = = Gethrev (i +1, i +PMID)) { Wu                     // -PST =PMID; About                 } $                 Else{ -PED = PMID-1; -                 } -             } A             //cout<<i<< "" <<pst<<endl; +ans = max (ans,2* PST +1); the             intQED = min (i-1, n +1-i), Qst =1; -              while(Qst <QED) { $                 intQmid = (Qst + QED +1) /2; the                 if(Geth (I-qmid, I-1) = = Gethrev (i, i + Qmid-1)){ the  theQst =Qmid; the                 } -                 Else{ inQED = Qmid-1; the                 } the             } Aboutans = max (ans,2*Qst); the  the         } the  +printf"Case %d:%d\n", cas++, ans); -     } the Bayi}

poj3974 palindrome "palindrome" "Hash"

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.