题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336
nex数组。
1 #include<cstdio> 2 #include<cstring> 3 const int maxn=200010; 4 char s[maxn]; 5 int nex[maxn]; 6 int n; 7 int main() 8 { 9 int t; 10 int cnt; 11 scanf("%d",&t); 12 while(t--) 13 { 14 cnt=0; 15 scanf("%d",&n); 16 scanf("%s",s); 17 int i=0,j=-1; 18 nex[0]=-1; 19 while(i<n) 20 { 21 if(j==-1||s[i]==s[j]){ 22 nex[++i]=++j; 23 if(nex[i]>0) cnt=(cnt+1)%10007; 24 } 25 else j=nex[j]; 26 27 } 28 printf("%d ",(cnt+n%10007)%10007); 29 } 30 }