Power Strings(求循环节)

来源:互联网 发布:笑话软件 编辑:程序博客网 时间:2024/05/17 20:31

题目描述

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).

输入

 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.

输出

 For each s you should print the largest n such that s = a^n for some string a.

示例输入

abcdaaaaababab.

示例输出

143

提示

 This problem has huge input, use scanf instead of cin to avoid time limit exceed.
#include<stdio.h>#include<string.h>char s[2000000];int next[2000000];int getnext(char st[],int next[]){    int i=0,j=-1;    next[0]=-1;    while(st[i]!='\0')        if(j==-1||st[i]==st[j])        {            i++;            j++;            next[i]=j;        }else        j=next[j];    if(j==0)        return -1;    else        return i-j;}int main(){    int len;    while(scanf("%s",s),strcmp(s,"."))    {        len=strlen(s);        int l=getnext(s,next);        if(l==-1||len%l)            printf("1\n");        else            printf("%d\n",len/l);    }    return 0;}


0 0