F - Power Strings

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.

这题不就是和上一个题类似,还简单点,直接就是(len)/n,len为字符串长度,n为循环节的长度,当len%n==0

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e6+10;
char s[maxn];
int ne[maxn];
void get(int len)
{
	ne[0]=-1;
	int j=-1;
	for(int i=0;i<len;)
	{
		if(j==-1||s[i]==s[j] )ne[++i]=++j;
		else j=ne[j];
	}
}
int main()
{
	while(~scanf("%s",s)&&s[0]!='.')
	{
		int len=strlen(s);
		get(len);
		int n=len-ne[len];//循环节的长度 
		if(len%n==0)  	 //中间没有杂数 
		printf("%d\n",len/n);
		else printf("1\n");
	}
}
/* 
   len=18;
   a  b a b c a b a b a b a  b   c a   b  a b 
-1 0 0 1 0 0 1 2 3 0 1 2  3   4 5   6  7 8  9
   0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 0 0 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 1 1
 */
  

猜你喜欢

转载自blog.csdn.net/qq_41286356/article/details/85029068