码迷,mamicode.com
首页 > 其他好文 > 详细

poj2406 Power Strings

时间:2014-12-14 21:14:42      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:des   blog   io   ar   sp   for   on   div   log   

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.
 
题意是给一个串s,求有没有一个串能通过复制自己变成s。求出最大的长度
这题跟那个bzoj1355很像……
最长的串的长度应该是n-next[n],然后判断n能不能整除n-next[n],如果能输出商,不能输出1
#include<cstdio>
#include<cstring>
#define LL long long
inline 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

标签:des   blog   io   ar   sp   for   on   div   log   

原文地址:http://www.cnblogs.com/zhber/p/4162960.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!