题目
https://www.luogu.org/problemnew/show/P2375
做法
查找多少个前缀与后缀配对,其实就是\(fail\)树的深度
而不可重叠,其实\(i\)不可用的,\(i+1\)也是不可用的,所以可以和\(kmp\)一样的做法继承前一位的再匹配
Code
#include<bits/stdc++.h>
typedef int LL;
const LL mod=1e9+7,maxn=1e6+9;
LL n;
LL ans[maxn],fail[maxn],num[maxn];
char s[maxn];
int main(){
scanf("%d",&n);
while(n--){
scanf(" %s",s+1);
LL len(strlen(s+1));
for(LL i=1;i<=len;++i)
fail[i]=ans[i]=num[i]=0;
for(LL i=2;i<=len;++i){
LL lst(fail[i-1]);
while(lst && s[lst+1]!=s[i])
lst=fail[lst];
if(s[lst+1]==s[i]){
fail[i]=lst+1;
}
ans[i]=ans[fail[i]]+1;
}
LL ret(1),lst(0);
for(LL i=2;i<=len;++i){
while(lst && s[lst+1]!=s[i])
lst=fail[lst];
if(s[lst+1]==s[i])
++lst;
while((i>>1)<lst) lst=fail[lst];
num[i]=ans[lst];
ret=1ll*ret*(num[i]+1)%mod;
}
printf("%d\n",ret);
}
return 0;
}