传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3670
题解:
对于每一位,先求出它的next和que数组
que表示的是对于字符串的前i个字符所构成的子串,需要多少次k=next[k]可以得到k=0:que[i]=que[next[i]]+1;
之后再用next数组往下匹配k=next[k],当i-k>k+1时,num[i]=que[k]
这道题就搞完了
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 1000010
#define mod 1000000007
using namespace std;
typedef long long ll;
ll ans;
int n;
int a[N];
int next[N],que[N];
char str[N];
int main()
{
int tot;
cin>>tot;
while(tot--)
{
scanf("%s",str+1);
int n=strlen(str+1);
for(int i=1;i<=n;i++)a[i]=str[i]-'a'+1;
int k=0,p=0;
ans=1,que[1]=1;
for(int i=2;i<=n;i++)
{
while(k>0&&a[i]!=a[k+1])k=next[k];
if(a[i]==a[k+1])k++;
next[i]=k;
que[i]=que[k]+1;
while(p>0&&(a[i]!=a[p+1]||i-p<=p+1))p=next[p];
if(a[i]==a[p+1])p++;
ans=(ll)ans*(que[p]+1)%mod;
}
cout<<ans<<endl;
memset(str,0,sizeof(str));
}
return 0;
}