标签:
| Time Limit: 3000MS | Memory Limit: 30000K | |
| Total Submissions: 14653 | Accepted: 6965 |
Description
Input
Output
Sample Input
3 aaa 12 aabaabaabaab 0
Sample Output
Test case #1 2 2 3 3 Test case #2 2 2 6 2 9 3 12 4
题意:求前缀的长度,以及其中最小循环节的循环次数 但是循环次数必须大于1
#include<stdio.h>
#include<string.h>
#define MAX 1000100
int next[MAX];
char str[MAX];
int a[MAX],b[MAX];
int n,m,k;
void getfail()
{
int i,j;
next[0]=next[1]=0;
for(i=1;i<n;i++)
{
j=next[i];
while(j&&str[i]!=str[j])
j=next[j];
next[i+1]=str[i]==str[j]?j+1:0;
}
}
void kmp()
{
int i,j;
for(i=1;i<=n;i++)
{
k=i;
if(k==(k-next[k]))//k-next[k]最小循环节长度
continue;
if(k%(k-next[k])==0)
printf("%d %d\n",i,k/(k-next[k]));
}
printf("\n");
}
int main()
{
int i,t=1;
while(scanf("%d",&n),n)
{
getchar();
for(i=0;i<n;i++)
scanf("%c",&str[i]);
getfail();
printf("Test case #%d\n",t++);
kmp();
}
return 0;
}
poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】
标签:
原文地址:http://www.cnblogs.com/tonghao/p/4712207.html