您的位置:首页 > 其它

POJ 2406 Power Strings【KMP求最小循环节/后缀数组】

2016-09-12 22:16 447 查看
 Power Strings

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 44612 Accepted: 18636
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.

题意:求一个字符串最多有几个相同的子串组成;

思路:KMP算法求最小循环节;

失误:这个题是基础题,可以有多种解法,看宇神还用MP算法,学完再说吧;

AC代码:

#include<cstdio>
#include<cstring>

const int MAXN=1e6+44;
int ne[MAXN];
char str[MAXN];

void Get_next(char *T,int *ne,int L)
{
ne[0]=-1;
int i=0,j=-1;
while(i<L)
{
if(j<0||T[i]==T[j]) ne[++i]=++j;
else j=ne[j];
}
}
int main()
{
while(~scanf("%c",&str[0]),str[0]!='.')
{
scanf("%s",str+1);
getchar();
int len=strlen(str);
Get_next(str,ne,len);
int ans=1;
if(len%(len-ne[len])==0&&ne[len]) ans=len/(len-ne[len]);
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: