Time Limit: 15000MS |
|
Memory Limit: 65536K |
Total Submissions: 5121 |
|
Accepted: 1834 |
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 |
About the Manacher algorithm .... ()
Code:
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <algorithm>5 #defineMAXN 10000106 CharSS[MAXN];7 CharSTR[MAXN <<1L];8 int var[Maxn <<1L];9 Ten intManacher () { One A - intLen =2, mx_l =0, pos =0; -str[0] ='$'; thestr[1] ='#'; - for(inti =0; Ss[i]! =' /'; i++) { -str[len++] =Ss[i]; -str[len++] ='#'; + } -Str[len] =' /'; + for(inti =0; i<len; i++) { A var[I] = mx_l>i? Std::min (var[2* Pos-i], mx_l-i):1; at while(Str[i +var[i]] = = Str[i-var[i]]) ++var[i]; - if(Mx_l<i +var[i]) { -mx_l = i +var[i]; -pos =i; - } - } in returnLen; - } to + intMain () { - the intCNT =1, Len =0, res =0; * while(SCANF ("%s", ss)! =EOF) { $ Panax Notoginseng if(ss[0] =='E') Break; -Len =Manacher (); theres =0; + for(inti =0; i<len; i++) ARes =std::max (RES,var[i]); thememset (str,0,sizeof(Char)*len); +printf"Case %d:%d\n", cnt++, res-1); - } $ return 0; $}
POJ----(3974) palindrome [longest palindrome string]