Power Strings POJ 2406 "KMP next App"

Source: Internet
Author: User
Tags printable characters

Description

Given 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 are defined in the normal way:a^0 = "" (The empty string) and a^ (n+1) = A * (a^n).

Input

Each test case was a line of input representing S, a string of printable characters. The length of S'll be is at least 1 and would not exceed 1 million characters. A line containing a period follows the last test case.

Output

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

Sample Input

Abcdaaaaababab.

Sample Output

143

Hint

This problem have huge input, use scanf instead of CIN to avoid time limit exceed.


Test instructions: The maximum number of occurrences of a substring in a source string

To summarize, if for I in the next array, I% (i-next[i]) = = 0 && next[i]! = 0, then the string loop is described, and

Circulation section length: I-next[i]

number of cycles: I/(I-next[i]) //Count not required next[i]! = 0 This condition


#include <cstdio> #include <cstring> #define MAX 1000100char str[max];int next[max];int len;void get_next () { int I=0,j=-1;next[0]=-1;while (I<len) {if (J==-1 | | str[i]==str[j]) {++i,++j;next[i]=j;} ELSEJ=NEXT[J];}} int main () {while (scanf ("%s", str), strcmp (str, ".")) {Len=strlen (str); Get_next (), if (len)% ((len)-next[len]) ==0) printf ("%d\n", (len)/(len)-next[len]), Else printf ("1\n");} return 0;}



#include <cstdio> #include <cstring> #define MAX 1000100char str[max];int next[max];int len;void get_next () { int I=0,j=-1;next[0]=-1;while (I<len) {if (J==-1 | | str[i]==str[j]) {++i,++j;next[i]=j;} ELSEJ=NEXT[J];}} int main () {while (scanf ("%s", str), strcmp (str, ".")) {Len=strlen (str); Get_next ();(len)% ((len)-next[len]) ==0&&next[len]!=0?printf ("%d\n", (len)/((len)-next[len])):p rintf ("1\n ");} return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Power Strings POJ 2406 "KMP next App"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.