首页 > 其他 > 详细

Power Strings(KMP)

时间:2014-02-21 08:23:25      阅读:400      评论:0      收藏:0      [点我收藏+]

http://poj.org/problem?id=2406

题意:计算一个串中重复因子出现的最多次数,即最多的循环节数。

bubuko.com,布布扣
 1 #include <stdio.h>
 2 #include <string.h>
 3 const int N=1000010;
 4 char str[N];
 5 int next[N];
 6 
 7 int get_next(char *s)
 8 {
 9     int j = 0,k = -1;
10     int len=strlen(s);
11     next[0]=-1;
12     while(j<len)
13     {
14         if(k==-1||s[k]==s[j])
15             next[++j]=++k;   
16         else
17             k = next[k];
18     }
19     j = len-k;
20     if (len%j==0)
21         return len/j;
22     else
23         return 1;
24 }
25 int main()
26 {
27     while(~scanf("%s",str))
28     {
29         if(str[0]==.)
30             break;
31         printf("%d\n",get_next(str));
32     }
33     return 0;
34 }
View Code

Power Strings(KMP)

原文:http://www.cnblogs.com/lahblogs/p/3558283.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!