标签:
http://acm.hdu.edu.cn/showproblem.php?pid=3336
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6672 Accepted Submission(s): 3089
1 | 2 | 3 | 4 | 5 | |
a | b | a | b | a | |
-1 | 0 | 0 | 1 | 2 | 3 |
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; #define MOD 10007 #define N 210000 char M[N]; int Next[N], dp[N]; void FindNext(int len) { int i=0, j=-1; Next[0] = -1; while(i<len) { if(j==-1 || M[i]==M[j]) Next[++i] = ++j; else j = Next[j]; } } int main() { int t; scanf("%d", &t); while(t--) { int n, i, Sum=0; memset(dp, 0, sizeof(dp)); scanf("%d%s", &n, M); FindNext(n); for(i=1; i<=n; i++) { dp[i] = (dp[Next[i]] + 1)%MOD; Sum = (dp[i]+Sum)%MOD; } printf("%d\n", Sum); } return 0; }
(KMP)Count the string -- hdu -- 3336
标签:
原文地址:http://www.cnblogs.com/YY56/p/4837698.html