Period
Time Limit: 1000MS Memory Limit: 65536KB
ProblemDescription
For each prefix of a givenstring 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. Thatis, for each i (2 ≤ i ≤ N) we want to know the largest K
> 1 (if there is one) suchthat the prefix of S with length i can be written as AK ,
that is A concatenated Ktimes, for some string A. Of course, we also want to
know the period K.
Input
The input file consistsof several test cases. Each test case consists of two
lines. The first one containsN (2 <= N <= 1 000 000) – the size of the string S.
The second line contains thestring 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 iand the period K separated by a single space; the
prefix sizes must be inincreasing order. Print a blank line after each test case.
ExampleInput
3
aaa
12
aabaabaabaab
0
ExampleOutput
Test case #1
2 2
3 3
Test case #2
2 2
6 2
9 3
12 4
Hint
Author
Southeastern EuropeanRegional Programming Contest
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
int next[2000000];
void qnext(char *s2,int next[],int len)
{
next[0] = -1;
int q = -1;
int h = 0;
while(h <= len-1)
{
if(q == -1||s2[q] == s2[h])
next[++h] = ++q;
else
q = next[q];
}
}
int kmp(char *s1,char *s2,int len1,int len2)
{
int top = 0;
int i = 0;
int j = 0;
int k;
while(i < len1)
{
if(j == -1|| s1[i] == s2[j])
{
i++;
j++;
}
else
j = next[j];
if(j == len2)
{
if(!top) k = i - j;
j = 0;
top ++;
}
}
if(top>=1)
return k;
else
return -1;
}
int main()
{
char s[2000000];
int n,i;
int o = 0;
while(scanf("%d",&n),n)
{
o++;
memset(next,0,sizeof(next[0]));
printf("Test case #%d\n",o);
scanf("%s",s);
qnext(s,next,n);
for(i=2;i<=n;i++)
{
int t = i - next[i];
if(i%t==0&&i/t>1)
printf("%d %d\n",i,i/t);
}
printf("\n");
}
return 0;
}
/***************************************************
User name: jk160505徐红博
Result: Accepted
Take time: 36ms
Take Memory: 1892KB
Submit time: 2017-02-08 22:30:22
****************************************************/