2406 Power Strings KMP 判断字符串最多由几部分组成

Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 14524   Accepted: 6098

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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
const int maxn=2000000;
int next[maxn];
char a[maxn],b[maxn];
//s数组从0开始,但是next数组计算时是从1开始(next[i]--s[i-1]),计算结果也从1开始
void get_next(char *s,int next[])
{
    int len=strlen(s);
    int i=0,j=-1;
    next[0]=-1;
    while(i<len)
    {
        if(j==-1||s[i]==s[j]) i++,j++,next[i]=j;
        else j=next[j];
    }
}
int main()
{
    while(gets(b))
    {
        if(strcmp(b,".")==0) break;
        get_next(b,next);//计算next
        int len=strlen(b);
        int res=len-next[len];//important
        printf("%d/n",len%res==0?len/res:1);//important
    }
    return 0;
}
 

你可能感兴趣的:(2406 Power Strings KMP 判断字符串最多由几部分组成)