POJ2406 Power Strings(KMP,循环节)

来源:互联网 发布:进销存软件破解版 编辑:程序博客网 时间:2024/05/21 10:06

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

思路

最小循环节的裸题,判断len就好

代码

#include<cstdio>#include<cstring>#include<string>#include<set>#include<iostream>#include<stack>#include<queue>#include<vector>#include<algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define mod 10000007#define debug() puts("what the fuck!!!")#define N 1000000+20#define ll longlongusing namespace std;char s[N];int nxt[N];void get_next(int len){    int j=0,k=-1;    nxt[0]=-1;    while(j<len)        if(k==-1||s[j]==s[k])            nxt[++j]=++k;        else            k=nxt[k];}int main(){    while(scanf("%s",s)&&s[0]!='.')    {        int len=strlen(s);        get_next(len);        int j=len-nxt[len];        if(len%j==0)            printf("%d\n",len/j);        else            printf("1\n");    }    return 0;}