POJ 2406(KMP next数组性质)

来源:互联网 发布:ds18b20单片机程序 编辑:程序博客网 时间:2024/06/05 09:11

G - Power Strings
Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

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.





题意:给你一个字符串,问最多可以分成最短构成子串,


题解:这里直接使用fail指针的性质,n-fail[n]就是最短的子串长度,这一题有毒,害我错了很多次,实际上也是考虑的不周全,如果n%(n-fail[n])!=0,那么就无法构成这样的子串比如ABABA就无法构成这样的串,所以还要判断一下能否整除。




#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define N  2000000char s[N];int fail[N];int ans;void getfail(char *p,int *f){int m=strlen(p);f[0]=0;f[1]=0;for(int i=1;i<m;i++){int j=f[i]; while(j&&p[i]!=p[j])j=f[j]; f[i+1]=p[i]==p[j]?j+1:0;}}int main(){#ifdef CDZSCfreopen("i.txt","r",stdin);#endifwhile(~scanf("%s",s)){if(s[0]=='.')break;getfail(s,fail);int len=strlen(s); ans=len%(len-fail[len])==0?len/(len-fail[len]):1;printf("%d\n",ans);}return 0;}






0 0