Palindrome
Time Limit: 15000MS | Memory Limit: 65536K | |
Total Submissions: 2786 | Accepted: 1021 |
Description
Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?"
A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not.
The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while 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 you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.
A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not.
The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while 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 you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.
Input
Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that 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
abcbabcbabcba abacacbaaaab END
Sample Output
Case 1: 13 Case 2: 6
Source
本题要求最长回文串,只是没卡的那么严,Manacher算法可过,后缀数组也可过,据说暴力的话如果二分长度也可过。
1.Manacher算法,时间复杂度为O(N)。
#include"iostream"
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=1000000+100;
char str1[MAXN*2],str2[MAXN*2];//待处理字符串
int num[MAXN*2];
//将str1变成str2,如abab变成$#a#b#a#b#
void init()
{
int i,id;
str2[0]='$';
str2[1]='#';
for(i=0,id=2;str1[i];i++,id+=2)
{
str2[id]=str1[i];
str2[id+1]='#';
}
str2[id]=0;
}
//Manacher算法求最长回文子串,时间复杂度为O(N)
int Manacher()
{
int i,ans=0,MxR=0,pos;
for(i=1;str2[i];i++)
{
if(MxR>i)num[i]=num[pos*2-i]<(MxR-i)?num[pos*2-i]:(MxR-i);
else num[i]=1;
while(str2[i+num[i]]==str2[i-num[i]])
num[i]++;
if(num[i]+i>MxR)
{
MxR=num[i]+i;
pos=i;
}
if(ans<num[i])
ans=num[i];
}
return ans-1;
}
int main()
{
int ans,tag=1;
while(~scanf("%s",str1))
{
if(strcmp(str1,"END")==0)break;
init();
ans=Manacher();
// printf("%d\n",ans);
printf("Case %d: %d\n",tag++,ans);
}
return 0;
}