poj 2406

Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 26943   Accepted: 11281

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

主要是next函数,才发现n个字符是n+1个next值(next0-----nextn),然后l%(l-nextn)==0代表字符串由l/(l-nextn)个相同字符串组成。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[1000005];
int next[1000005];
void getnext()
{
    int l=strlen(s);
    int j=0,k=-1;
    next[0]=-1;
    while(j<l)
    {
        if(k==-1||s[j]==s[k])
        {
            j++;
            k++;
            next[j]=k;
        }
        else k=next[k];
    }
    int h=l-k;
    if(l%h==0)cout<<l/h<<endl;
    else cout<<1<<endl;
}
int main()
{
    while(scanf("%s",&s))
    {
        if(s[0]=='.')return 0;
        getnext();
    }
}


你可能感兴趣的:(poj,Baoge)