manacher算法----O(n)最长回文串

来源:互联网 发布:冲突世界 知乎 编辑:程序博客网 时间:2024/06/07 17:34

manacher算法----O(n)最长回文串

   

manacher的时间复杂度为O(n),后缀数组好像可以处理O(nlogn),但是有些变态题目可能卡logn。不过这个算法还算比较容易理解的。


算法基本要点:首先用一个非常巧妙的方式,将所有可能的奇数/偶数长度的回文子串都转换成了奇数长度:在每个字符的两边都插入一个特殊的符号。比如 abba 变成 #a#b#b#a#, aba变成 #a#b#a#。 为了进一步减少编码的复杂度,可以在字符串的开始加入另一个特殊字符,这样就不用特殊处理越界问题,比如$#a#b#a#。

下面以字符串12212321为例,经过上一步,变成了 S[] = "$#1#2#2#1#2#3#2#1#";

然后用一个数组 P[i] 来记录以字符S[i]为中心的最长回文子串向左/右扩张的长度(包括S[i]),比如S和P的对应关系:

S     #  1  #  2  #  2  #  1  #  2  #  3  #  2  #  1  #
P     1   2  1  2  5   2  1  4   1  2  1  6   1  2   1  2  1
(p.s. 可以看出,P[i]-1正好是原字符串中回文串的总长度)【因为添加的#要比原回文字符串多1,所以减1】

下面计算P[i],该算法增加两个辅助变量id和mx,其中id表示最大回文子串中心的位置,mx则为id+P[id],也就是最大回文子串的边界。

这个算法的关键点就在这里了:如果mx > i,那么P[i] >= MIN(P[2 * id - i], mx - i)。

具体代码如下:

复制代码
if(mx > i){      p[i] = (p[2*id - i] < (mx - i) ? p[2*id - i] : (mx - i));}else{       p[i] = 1;}
复制代码

当 mx - i > P[j] 的时候,以S[j]为中心的回文子串包含在以S[id]为中心的回文子串中,由于 i 和 j 对称,以S[i]为中心的回文子串必然包含在以S[id]为中心的回文子串中,所以必有 P[i] = P[j],见下图。

当 P[j] > mx - i 的时候,以S[j]为中心的回文子串不完全包含于以S[id]为中心的回文子串中,但是基于对称性可知,下图中两个绿框所包围的部分是相同的,也就是说以S[i]为中心的回文子串,其向右至少会扩张到mx的位置,也就是说 P[i] >= mx - i。至于mx之后的部分是否对称,就只能一个一个匹配了。

对于 mx <= i 的情况,无法对 P[i]做更多的假设,只能P[i] = 1,然后再去匹配了

下面给出原文,进一步解释算法为线性的原因


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include<stdio.h>  
  2. #include<string.h>  
  3. #include <iostream>  
  4. #include <cstring>  
  5. #include <cmath>  
  6. #include <queue>  
  7. #include <cstdlib>  
  8. using namespace std;  
  9. #define N 100010  
  10. typedef long long ll;  
  11.   
  12. void manacher(string& str) {  
  13.     int *p=new int[str.size()+1];  
  14.     memset(p,0,sizeof(p));  
  15.     int mx=0,id=0;  
  16.     for(int i=1; i<str.size()-1; i++) {  
  17.         if(mx>i) {  
  18.             p[i]=min(p[2*id-i],mx-i);  
  19.         } else p[i]=1;  
  20.         while(str[i-p[i]]==str[i+p[i]])  
  21.             p[i]++;  
  22.         if(i+p[i]>mx) {  
  23.             mx=i+p[i];  
  24.             id=i;  
  25.         }  
  26.   
  27.     }  
  28.     int max=0,ii;  
  29.     for(int i=1; i<str.size(); i++) {  
  30.         if(p[i]>max) {  
  31.             ii=i;  
  32.             max=p[i];  
  33.         }  
  34.     }  
  35.     max--;  
  36.     int start=ii-max;  
  37.     int end=ii+max;  
  38.     for(int i=start; i<=end; i++) {  
  39.         if(str[i]!='#') {  
  40.             cout<<str[i];  
  41.         }  
  42.     }  
  43.     cout<<endl;  
  44.   
  45.     delete p;  
  46. }  
  47. int main() {  
  48. #ifndef ONLINE_JUDGE  
  49.     freopen("in.txt","r",stdin);  
  50. #endif  
  51.     string str="12213553132123";  
  52.     string s;  
  53.     s+="$#";  
  54.     for(int i=0; i<str.size(); i++) {  
  55.         s+=str[i];  
  56.         s+="#";  
  57.     }  
  58.     s+="*";  
  59.     cout<<s<<endl;  
  60.     manacher(s);  
  61.     return 0;  
  62. }  
hdu3068

最长回文

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17168    Accepted Submission(s): 6306


Problem Description
给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.
回文就是正反读都是一样的字符串,如aba, abba等
 

Input
输入有多组case,不超过120组,每组输入为一行小写英文字符a,b,c...y,z组成的字符串S
两组case之间由空行隔开(该空行不用处理)
字符串长度len <= 110000
 

Output
每一行一个整数x,对应一组case,表示该组case的字符串中所包含的最长回文长度.
 

Sample Input
aaaaabab
 

Sample Output
43

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include<stdio.h>  
  2. #include<string.h>  
  3. #include <iostream>  
  4. #include <string>  
  5. #include <cmath>  
  6. #include <queue>  
  7. #include <cstdlib>  
  8. using namespace std;  
  9. #define N 2000000  
  10. typedef long long ll;  
  11. int p[N];  
  12. void manacher(string str) {  
  13.       
  14.     memset(p,0,sizeof(p));  
  15.     int mx=0,id=0;  
  16.     for(int i=1; i<str.size()-1; i++) {  
  17.         if(mx>i) {  
  18.             p[i]=min(p[2*id-i],mx-i);  
  19.         } else p[i]=1;  
  20.         while(str[i-p[i]]==str[i+p[i]])  
  21.             p[i]++;  
  22.         if(i+p[i]>mx) {  
  23.             mx=i+p[i];  
  24.             id=i;  
  25.         }  
  26.   
  27.     }  
  28.     int max=0,ii;  
  29.     for(int i=1; i<str.size(); i++) {  
  30.         if(p[i]>max) {  
  31.             ii=i;  
  32.             max=p[i];  
  33.         }  
  34.     }  
  35.     max--;  
  36.     /*int start=ii-max; 
  37.     int end=ii+max; 
  38.     for(int i=start; i<=end; i++) { 
  39.         if(str[i]!='#') { 
  40.             cout<<str[i]; 
  41.         } 
  42.     }*/  
  43.     cout<<max<<endl;  
  44. }  
  45. int main() {  
  46. #ifndef ONLINE_JUDGE  
  47.     freopen("in.txt","r",stdin);  
  48. #endif  
  49.     string s,str;  
  50.     while(cin>>s) {  
  51.         //cout<<s<<endl;  
  52.         str="$#";  
  53.         for(int i=0; i<s.size(); i++) {  
  54.             str+=s[i];  
  55.             str+="#";  
  56.         }  
  57.         str+="*";  
  58.         //cout<<str<<endl;  
  59.         manacher(str);  
  60.         getchar();  
  61.     }  
  62.     return 0;  
  63. }  

不要轻易在代码中使用动态分配,按照原先代码使用动态分配Wa了,也不知为什么


参考连接:

点击打开链接

0 0
原创粉丝点击