标签:
#include<stdio.h> #include<string.h> const int MAXN = 1e6+7; const int oo = 1e9+7; const int mod = 10007; char s[MAXN]; int next[MAXN], dp[MAXN]; void GetNext(char s[], int N) { int i=0, j=-1; next[0] = -1; while(i < N) { if(j==-1 || s[i]==s[j]) next[++i] = ++j; else j = next[j]; } } int main() { int T, N, ans; scanf("%d", &T); while(T--) { scanf("%d%s", &N, s); GetNext(s, N); next[N+1] = -1; ans = dp[0] = 0; 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; }
Count the string - HDU 3336(next+dp)
标签:
原文地址:http://www.cnblogs.com/liuxin13/p/4732297.html