hdu 3336
如果$next[j] = i$, 那么说明$[0, i-1]$与$[j-i, j-1]$部分相同
并且$i,j$之间没有更多的前缀
设$dp[i]$为前$i$个字符中前缀出现次数
$dp[i] = dp[next[i]]+1 $
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define ms(i,j) memset(i, j, sizeof i);
const int MAXN = 200000 + 5;
char s[MAXN];
int f[MAXN], dp[MAXN], n;
void init()
{
scanf("%d", &n);
scanf("%s", s);
}
void solve()
{
f[0] = f[1] = 0;
for (int i=1;i<n;i++)
{
int j = f[i];
while (j && s[i]!=s[j]) j = f[j];
f[i+1] = (s[i]==s[j]) ? (j+1) : (0);
}
ms(dp, 0);
int ans = 0;
for (int i=1;i<=n;i++)
{
dp[i] = dp[f[i]] + 1;
ans = (ans+dp[i]) % 10007;
}
printf("%d\n", ans);
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
init();
solve();
}
return 0;
}