POJ1916 Period KMP

本文介绍了一种基于KMP算法的字符串周期性检测方法,通过简洁的代码实现了对字符串中具有周期性的前缀子串的查找,并输出了子串长度及周期重复次数。

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

判断某一字符串中 , 哪些前缀子串有周期 , 输出子串长度以及子串中周期重复的次数 ( 次数>1 ) 
需要的只是对KMP性质的进一步理解...代码很短如下
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 using namespace std;
 8 const int maxn=1000010;
 9 const double eps=1e-8;
10 const long long modn=1000;
11 char a[maxn]={};
12 int nex[maxn]={};
13 int size[maxn]={};
14 int n,m;
15 void doit(){
16     int j=-1,i=0;
17     nex[0]=-1;
18     int z;
19     while(i<n){
20         if(j==-1||a[j]==a[i]){
21             nex[++i]=++j;
22             if(i%(i-j)==0&&i/(i-j)>1){
23                 printf("%d %d\n",i,i/(i-j));
24             }
25         }else{
26             j=nex[j];
27         }
28     }
29 }
30 int main(){
31     int K=0;
32     while(~scanf("%d",&n)&&n!=0){
33         ++K;
34         memset(nex,0,sizeof(nex));
35         memset(size,0,sizeof(size));
36         scanf("%s",a);
37         printf("Test case #%d\n",K);
38         doit();
39         cout<<endl;
40     }
41     return 0;
42 }
View Code

 

转载于:https://www.cnblogs.com/137shoebills/p/7786480.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值