HDU 4763 (KMP算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763

题目大意:给定一串字符,从中找出符合“EAEBE”格式的E的最大字符数。AB可以是任意数量的任意字符(a-z)。

Sample Input
5
xy
abc
aaa
aaaaba
aaxoaaaaa
 
Sample Output
1
1
2

分析:首先枚举判断开始和结尾是否满足作为E,再KMP计算中间是否存在E

代码如下:

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 1000001
int next[N];
char a[N]; void getnext(int m){
int i=,j=-;
next[]=-;
while(i<=m){
if(j==-||a[i]==a[j])
{
i++;
j++;
next[i]=j;
}
else j=next[j];
}
}
bool kmp(int st,int l){
int j=;
int i=st+;
int ed=l-st-;
while(i<ed)
{
if(j==-||a[i]==a[j]){
i++;
j++;
if(j==st+) return true;
}
else j=next[j];
}
return false;
}
bool judge(int ans,int l){
for(int i=;i<=ans;i++)
{
if(a[i]!=a[l-ans+i-]) return false;
}
return true;
}
int getsolve(){
int l=strlen(a);
int ans=;
getnext(int(l/)-);
if(l<) return ;
for(int i=l/-;i>=;i--){
if(judge(i,l)){
if(kmp(i,l))
{
ans=i+;
break;
}
}
}
return ans;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%s",a);
printf("%d\n",getsolve());
}
return ;
}
上一篇:ffmpeg-20160714-git-bin.7z


下一篇:apache hadoop 伪分布式安装