【POJ 2406 Power Strings】

时间:2023-12-29 14:28:02

Time Limit: 3000MS
Memory Limit: 65536K

Description

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Input

Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

For each s you should print the largest n such that s = a^n for some string a.

Sample Input

abcd
aaaa
ababab
.

Sample Output

1
4
3

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.

Source

Waterloo local 2002.07.01

题解:
      ①找出最小循环节————KMP

#include<stdio.h>
#include<cstring>
#define go(i,a,b) for(int i=a;i<=b;i++)
const int N=4000003;int m,f[N],j;char P[N];
int main()
{
f[0]=f[1]=0;
while(scanf("%s",P),m=strlen(P),P[0]!='.')
{
go(i,1,m-1){j=f[i];while(j&&P[i]!=P[j])j=f[j];f[i+1]=P[i]==P[j]?j+1:0;}
printf("%d\n",m%(m-f[m])?1:m/(m-f[m]));
}
return 0;
}//Paul_Guderian

镜子里面,像看到人生终点,或许再过上几年

你也有张虚伪的脸,难道我们,是为了这样,才来到这世上……—————《你曾是少年》