题意:一个字符串,求符合EAEBE形式情况下最大E子串的长度
思路:前缀E和后缀E可以用next数组求出,然后在判断中间的E是否存在,具体做法是:next[len]=i,在[2 * i ,len - i](因为不能重合)内找是否有next[j]=i,存在则i就为答案,不存在的话令i=next[i],而不是i--,继续找
#include <stdio.h>
#include <string.h>
const int LEN = 1000005;
char str[LEN];
int next[LEN];
void getnext(int len,char *str){
next[1] = 0;
for(int j = 0, i = 2; i <= len; i++){
while(j != 0 && str[i] != str[j + 1]){
j = next[j];
}
if(str[i] == str[j + 1]){
j++;
}
next[i] = j;
}
}
int main(void){
int T;
scanf("%d",&T);
while(T--){
scanf("%s",str + 1);
int len = strlen(str + 1);
getnext(len,str);
int ans = 0;
for(int i = next[len]; i; i = next[i]){
for(int j = i << 1; j <= len - i; j++){
if(next[j] == i){
ans = i;
break;
}
}
if(ans){
break;
}
}
printf("%d\n",ans);
}
return 0;
}