POJ 1961 Period(KMP)

时间:2023-12-02 22:24:44

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

题意 :给你一个字符串,让你输出到第几个字符时,循环结的个数。

思路 :这个题和2409差不多,稍微修改一下,加一个循环就行了,用的也是KMP。

#include <string.h>
#include <stdio.h>
#include <iostream> using namespace std ; const int maxn = ; char ch[maxn] ;
int next[maxn] ; int main()
{
int n ;
int test = ;
while(scanf("%d%*c",&n)!=EOF)
{
if(n == ) break ;
scanf("%s",ch) ;
printf("Test case #%d\n",test) ;
test++ ;
int i = , j = - ;
next[] = - ;
while(i < n )
{
if(j == - || ch[i] == ch[j])
next[++i] = ++j ;
else
j = next[j] ;
}
int len ;
for(int k = ; k <= n ; k++)
{
len = k - next[k] ;
if(k != len && k % len == )
printf("%d %d\n",k,k / len) ;
}
printf("\n") ;
}
return ;
}