首页 > 代码库 > Palindrome(最长回文串manacher算法)O(n)
Palindrome(最长回文串manacher算法)O(n)
Palindrome
Time Limit:15000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uDescription
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
abcbabcbabcbaabacacbaaaabEND
Sample Output
Case 1: 13Case 2: 6
1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 using namespace std; 6 const int maxn=1000000+10; 7 //manacher算法 8 const int LEN=maxn; 9 const int N=LEN*2;10 int p[N];11 char str[LEN],tmp[N];12 //p[i]表示以str[i]为中心的回文往右延伸的 最长长度13 void manacher(char* str, int* p)14 {15 int n=strlen(str), i, id, mx;16 for(p[mx=id=0]=i=1; i<n; i++)17 {18 p[i]=mx>i?min(p[2*id-i], mx-i+1):1;19 while(i+p[i]<n&&i-p[i]>=0&&str[i+p[i]]==str[i-p[i]]) p[i]++;20 if(mx<i+p[i]-1)21 {22 id=i;23 mx=i+p[i]-1;24 }25 }26 }27 //求str的最长回文的长度28 int maxPalindrome(char* str)29 {30 int ans=0;31 int i, n;32 for(i=0, n=strlen(str); i<n; i++)33 {34 tmp[2*i]=‘#‘;35 tmp[2*i+1]=str[i];36 }37 tmp[2*i]=‘#‘;38 tmp[2*i+1]=‘\0‘;39 n=n*2+1;40 manacher(tmp, p);41 for(i=0; i<n; i++)42 {43 if(ans < p[i]-1)44 {45 ans = p[i]-1;46 }47 }48 return ans;49 }50 int main()51 {52 int i=1;53 for(i=1;; i++)54 {55 scanf("%s",str);56 char s[]= {‘E‘,‘N‘,‘D‘,‘\0‘};57 if(strcmp(str,s)==0)58 break;59 else60 printf("Case %d: %d\n",i,maxPalindrome(str));61 }62 return 0;63 }
Palindrome(最长回文串manacher算法)O(n)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。