const int N = 1e6 + 5;
int nxt[N];
char s[N], t[N];
int num[N];
void kmp_pre(char s[], int n)
{
nxt[0]=-1,nxt[1] = 0;
num[1] = 1;
for(int i = 2,j=0; i <= n; i ++)
{
while(j && s[i] != s[j + 1]) j = nxt[j];
if(s[j + 1] == s[i])j ++;
num[i] = num[j]+1;
nxt[i] = j;
}
}
vector<int> kmp(char s[],int n,char t[],int m)
{
kmp_pre(s,n);
vector<int> res;
for(int i = 1,j=0; i <=m; i ++)
{
while(j > 0 && s[j + 1] != t[i]) j = nxt[j];
if(s[j + 1] == t[i]) j ++;
if(j == n)
{
res.emplace_back(i - n + 1);
j = nxt[j];
}
}
return res;
}
int main()
{
//freopen("in.txt", "r", stdin);
int _;_ = in();
while (_--)
{
scanf("%s", s + 1);
int ls = strlen(s + 1);
kmp_pre(s, ls);
ll ans = 1;
int j = 0;
for (int i = 2; i <= ls; i++)
{
while (j && s[i] != s[j + 1])j = nxt[j];
if (s[j + 1] == s[i])j++;
while ((j << 1) > i)j = nxt[j];
ans = ans * (num[j] + 1) % mod;
}
cout << ans << endl;
}
return 0;
}
P2375 [NOI2014]动物园(kmp)
最新推荐文章于 2021-07-07 16:16:02 发布