hdoj 3336

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336

运用kmp的next数组后,利用dp滚动可以求得答案。

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 const int maxn=200006;
 5 char str[maxn];
 6 int dp[maxn],next[maxn];
 7 int n;
 8 
 9 void get_next()
10 {
11     next[0]=-1;
12     int temp=-1,i=0;
13     while(i!=n){
14         if(temp==-1 || str[temp]==str[i])
15             next[++i]=++temp;
16         else temp=next[temp];
17     }
18 }
19 
20 int main()
21 {
22     int T;
23     scanf("%d",&T);
24     while(T--){
25         scanf("%d%s",&n,str);
26         get_next();
27         memset( dp, 0, sizeof dp);
28         int ans=n;
29         for(int i=0;i<=n;i++){
30             if(next[i]<=0) continue;
31             dp[i]=dp[next[i]]+1;
32             ans+=dp[i];
33         }
34         printf("%d
",ans%10007);
35     }
36     return 0;
37 }
原文地址:https://www.cnblogs.com/ZQUACM-875180305/p/9289000.html