码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3336 (KMP next性质) Count the string

时间:2014-11-30 21:19:45      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

直接上传送门好了,我觉得他分析得非常透彻。

http://972169909-qq-com.iteye.com/blog/1114968

bubuko.com,布布扣
 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 const int maxn = 200000 + 10;
 5 const int MOD = 10007;
 6 char s[maxn];
 7 int next[maxn], l;
 8 
 9 void get_next()
10 {
11     int k = -1, j = 0;
12     next[0] = -1;
13     while(j < l)
14     {
15         if(k == -1 || s[k] == s[j])
16         {
17             k++;
18             j++;
19             next[j] = k;
20         }
21         else k = next[k];
22     }
23 }
24 
25 int main(void)
26 {
27     int T;
28     scanf("%d", &T);
29     while(T--)
30     {
31         scanf("%d", &l);
32         scanf("%s", s);
33         get_next();
34         
35         int ans = 0;
36         for(int j = 0; j < l; ++j)
37             if(next[j] > 0 && next[j] + 1 != next[j+1])
38                 ans = (ans + next[j]) % MOD;
39         ans = (ans + l + next[l]) % MOD;
40         printf("%d\n", ans);
41     }
42     
43     return 0;
44 }
代码君

 

HDU 3336 (KMP next性质) Count the string

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/AOQNRMGYXLMV/p/4133572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!