设dp【i】:以string[i]结尾的子串总共含前缀的数量
所以dp[j]=dp[i]+1,即以i结尾的子串中含前缀的数量加上前j个字符这一前缀
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <vector>
using namespace std;
#define mod 10007
#define maxn 400000
int next[maxn],n,dp[maxn];
char st[maxn];
void getnext()
{
next[0]=0;
for(int i=1;i<n;i++)
{
int j=next[i];
while(j&&st[j]!=st[i])
{
j=next[j];
}
if(st[j]==st[i])
{
next[i+1]=j+1;
}
else
next[i+1]=0;
}
}
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
int ans=0;
memset(dp,0,sizeof(dp));
scanf("%d",&n);
scanf("%s",st);
getnext();
for(int i=1;i<=n;i++)
{
dp[i]=(dp[next[i]]+1)%mod;
ans=(ans+dp[i])%mod;
}
printf("%d\n",ans);
}
return 0;
}