LA_3026_Period_(kmp)

时间:2021-06-20 20:44:30

描述


https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1027

给出一个长度为n的字符串,求它的每个前缀(前缀长度>=2)是否是循环的(循环次数>1),如果是,求出循环周期.

分析


我们先来看对于一个满足条件的循环的串.设其长度为l,一个循环节的长度为s,那么很明显,[1,l-s]与[s+1,l]这一对前后缀是相同的,并且l是s的整数倍.

那么反过来呢?如果一个串的前后缀[1,l-s]与[s+1,l]相同,那么该串从第一位开始,每一位都和其右移s位的相同,如果l是s的整数倍,那么(l-s)就是s的整数倍,那么前缀可以分成整数个长度为s的字串,每一个字串都是右移s后重合的,那么这就是一个循环节为s的循环串.

这样的话这两种定义就是等价的,所以我们直接用kmp求前后缀重叠就行了.

 #include <bits/stdc++.h>
using namespace std; const int maxn=+;
int n,kase;
char p[maxn];
int f[maxn];
void get_fail(){
f[]=; f[]=;
for(int i=;i<n;i++){
int j=f[i];
while(j&&p[i]!=p[j]) j=f[j];
f[i+]=p[i]==p[j]?j+:;
}
}
int main(){
while(scanf("%d",&n)&&n){
scanf("%s",p);
get_fail();
printf("Test case #%d\n",++kase);
for(int i=;i<=n;i++)
if(f[i]>&&i%(i-f[i])==) printf("%d %d\n",i,i/(i-f[i]));
printf("\n");
}
return ;
}

3026
Period
For each prefix of a given string S with N characters (each character has an ASCII code between 97 and
126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 ≤ i ≤ N )
we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be
written as A K , that is A concatenated K times, for some string A. Of course, we also want to know
the period K.
Input
The input file consists of several test cases. Each test case consists of two lines. The first one contains
N (2 ≤ N ≤ 1000000) the size of the string S. The second line contains the string S. The input file
ends with a line, having the number zero on it.
Output
For each test case, output ‘Test case #’ and the consecutive test case number on a single line; then, for
each prefix with length i that has a period K > 1, output the prefix size i and the period K separated
by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.
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