您的位置:首页 > 其它

POJ2406 Power Strings

2016-07-17 09:07 197 查看

 

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 42637   Accepted: 17787

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求出next数组,设字符串长度为len,若len/(len-next[len])为整数,那么这个数就是答案。

类似的题见http://www.cnblogs.com/SilverNebula/p/5550595.html

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int next[1000010];
char s[1000010];
int len,ans;
void getnext(){
int i,j;
next[0]=0;
next[1]=0;
for(i=1,j=0;i<len;i++){
while(s[j]!=s[i] && j)j=next[j];
if(s[j]==s[i])j++;
next[i+1]=j;
}
return;
}
int main(){
while(scanf("%s",s)!=EOF){
if(s[0]=='.')break;
len=strlen(s);
//        memset(next,0,sizeof next);
getnext();
ans=0;
if(len%(len-next[len])==0)
printf("%d\n",len/(len-next[len]));
else printf("1\n");
}
return 0;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: