标签:des style blog http color os io java ar
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1012 Accepted Submission(s): 358
简单的manacher算法:
代码:
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #define maxn 100002 5 int str[maxn<<1]; 6 int ra[maxn<<1]; 7 int min(int a,int b){ 8 return a<b?a:b; 9 } 10 int manacher(int len){ 11 12 int mx=0,ce=0,ans=0; 13 memset(ra,0,sizeof(int)*(len+1)); 14 for(int i=1;i<=len;i++){ 15 if(mx>i)ra[i]=min(ra[ce*2-i],mx-i); 16 else ra[i]=1; 17 while(str[i-ra[i]]==str[i+ra[i]]&&(str[i+ra[i]-2]==1||str[i+ra[i]-2]>=str[i+ra[i]])){ 18 ra[i]++; 19 } 20 if(i+ra[i]>mx){ 21 mx=i+ra[i]; 22 ce=i; 23 } 24 if(ans<ra[i])ans=ra[i]; 25 } 26 return ans; 27 } 28 29 int main(){ 30 int cas,n; 31 // freopen("test.in","r",stdin); 32 // system("call test.in"); 33 scanf("%d",&cas); 34 str[0]=-1; 35 while(cas--){ 36 scanf("%d",&n); 37 int len=(n<<1); 38 for(int i=2;i<=len;i+=2){ 39 str[i-1]=1; 40 scanf("%d",&str[i]); 41 } 42 str[len+1]=1; 43 printf("%d\n",manacher(len+1)-1); 44 } 45 }
hdu----(4513)吉哥系列故事——完美队形II(manacher(最长回文串算法))
标签:des style blog http color os io java ar
原文地址:http://www.cnblogs.com/gongxijun/p/3959413.html