【HDOJ】【3068】最长回文

Manacher算法


  Manacher模板题……

 1 //HDOJ 3068
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<iostream>
 6 #include<algorithm>
 7 #define rep(i,n) for(int i=0;i<n;++i)
 8 #define F(i,j,n) for(int i=j;i<=n;++i)
 9 #define D(i,j,n) for(int i=j;i>=n;--i)
10 using namespace std;
11 typedef long long LL;
12 inline int getint(){
13     int r=1,v=0; char ch=getchar();
14     for(;!isdigit(ch);ch=getchar()) if(ch=='-')r=-1;
15     for(; isdigit(ch);ch=getchar()) v=v*10+ch-'0';
16     return r*v;
17 }
18 const int N=1e5+10,INF=~0u>>2;
19 /*******************template********************/
20 char b[N];
21 int p[N<<1],a[N<<1];
22 int main(){
23 #ifndef ONLINE_JUDGE 
24     freopen("3068.in","r",stdin);
25 //    freopen("3068.out","w",stdout);
26 #endif
27     int n,id,mx,ans;
28     while(scanf("%s",b)!=EOF){
29         n=strlen(b);
30         memset(p,0,sizeof p);
31         F(i,1,n) a[i<<1]=b[i-1];
32         n=n<<1|1;
33         id=mx=ans=0;
34         F(i,1,n){
35             if (mx>i) p[i]=min(p[2*id-i],mx-i);
36             while(i-p[i]-1>0 && i+p[i]+1<=n && a[i-p[i]-1]==a[i+p[i]+1]) p[i]++;
37             if (p[i]+i>mx) mx=p[i]+i,id=i;
38             if (p[i]>ans) ans=p[i];
39         }
40         printf("%d\n",ans);
41     }
42     return 0;
43 }
View Code

最长回文

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


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
aaaa abab
 

 

Sample Output
4 3
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1358 1686 3336 3065 3746 
 

 

Statistic | Submit | Discuss | Note
posted @ 2015-04-07 10:42  Tunix  阅读(171)  评论(0编辑  收藏  举报