poj 2406 Power Strings(kmp)

来源:互联网 发布:鹿晗 知乎 编辑:程序博客网 时间:2024/05/17 03:05

http://poj.org/problem?id=2406

Power Strings

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
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <cstdlib>#include <limits>#include <queue>#include <stack>#include <vector>#include <map>using namespace std;#define N 1010000#define INF 0x3f3f3f3f#define PI acos (-1.0)#define EPS 1e-8#define met(a, b) memset (a, b, sizeof (a))char s[N];int Next[N];void get_next (char s[], int n, int Next[]);int main (){    while (scanf ("%s", s), strcmp (s, "."))    {        int len = strlen (s);        get_next (s, len, Next);        int l = len - Next[len];        if (len % l) puts ("1");        else            printf ("%d\n", len/l);    }    return 0;}void get_next (char s[], int n, int Next[]){    int i = 0, j = -1;    Next[i] = -1;    while (i < n)    {        if (j == -1 || s[i] == s[j])            Next[++i] = ++j;        else j = Next[j];    }    return;}


1 0
原创粉丝点击