首页 > 代码库 > poj2406 Power Strings

poj2406 Power Strings

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

abcdaaaaababab.

Sample Output

143

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.
 
题意是给一个串s,求有没有一个串能通过复制自己变成s。求出最大的长度
这题跟那个bzoj1355很像……
最长的串的长度应该是n-next[n],然后判断n能不能整除n-next[n],如果能输出商,不能输出1
#include<cstdio>#include<cstring>#define LL long longinline void write(LL a){	if (a<0){printf("-");a=-a;}	if (a>=10)write(a/10);	putchar(a%10+‘0‘);}inline void writeln(LL a){write(a);printf("\n");}int next[1000010];char s[1000010];int l,j,k;inline void pre(){	memset(next,0,sizeof(next));	j=0;	for (int i=2;i<=l;i++)	{	  	while (j>0&&s[j+1]!=s[i])j=next[j];	  	if (s[j+1]==s[i])j++;	  	next[i]=j;	}}int main(){	while (scanf("%s",s+1)&&s[1]!=‘.‘)	{		l=strlen(s+1);		pre();		int len=l-next[l];		if (l%len)writeln(1);		else writeln(l/len);	}	return 0;}

 

poj2406 Power Strings