https://vjudge.net/contest/70325#problem/K HDU 3336
题意:求字符串所有前缀在原串出现的次数
思路:对自己求extend数组,遍历一遍相加就是结果.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=200000+5;
char mode[maxn];
int nextt[maxn];
void getNext(char mode[],int nextt[],int modeLen)
{
int i,a,p;
a=p=0;nextt[0]=modeLen;
for(i=1;i<modeLen;i++)
{
if(i>=p || i+nextt[i-a]>=p)
{
if(i>=p) p=i;
while(p<modeLen && mode[p]==mode[p-i]) ++p;
nextt[i]=p-i;
a=i;
}
else nextt[i]=nextt[i-a];
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf(" %c",&mode[i]);
}
mode[n]='\0';
int modeLen=strlen(mode);
getNext(mode,nextt,modeLen);
long long ans=0;
for(int i=0;i<modeLen;i++)
{
ans=(ans+nextt[i]%10007)%10007;
}
printf("%lld\n",ans);
}
return 0;
}