【HDOJ】【3068】最长回文

时间:2023-03-08 15:56:53
【HDOJ】【3068】最长回文

Manacher算法


  Manacher模板题……

 //HDOJ 3068
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
using namespace std;
typedef long long LL;
inline int getint(){
int r=,v=; char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-')r=-;
for(; isdigit(ch);ch=getchar()) v=v*+ch-'';
return r*v;
}
const int N=1e5+,INF=~0u>>;
/*******************template********************/
char b[N];
int p[N<<],a[N<<];
int main(){
#ifndef ONLINE_JUDGE
freopen("3068.in","r",stdin);
// freopen("3068.out","w",stdout);
#endif
int n,id,mx,ans;
while(scanf("%s",b)!=EOF){
n=strlen(b);
memset(p,,sizeof p);
F(i,,n) a[i<<]=b[i-];
n=n<<|;
id=mx=ans=;
F(i,,n){
if (mx>i) p[i]=min(p[*id-i],mx-i);
while(i-p[i]-> && i+p[i]+<=n && a[i-p[i]-]==a[i+p[i]+]) p[i]++;
if (p[i]+i>mx) mx=p[i]+i,id=i;
if (p[i]>ans) ans=p[i];
}
printf("%d\n",ans);
}
return ;
}

最长回文

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