题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 200005
using namespace std;
const int mod=10007;
char s[N];
int n,ne[N];
void get_next() {
int i=0,j=-1;
ne[0]=-1;
while(i<n) {
if(j==-1||s[i]==s[j]) {
i++;
j++;
ne[i]=j;
} else
j=ne[j];
}
}
int main() {
int t;
scanf("%d",&t);
while(t--) {
scanf("%d",&n);
scanf("%s",s);
get_next();
int ans=0;
for(int i=1; i<=n; i++) {
if(ne[i]>0) ans++;
if(ans>=mod)
ans%=mod;
}
ans+=n;//加上n
ans%=mod;
printf("%d\n",ans);
}
}