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"