Title Link: http://poj.org/problem?id=2406
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.
Source
Waterloo local 2002.07.01 give you a string, this string is inside of a substring loop n times obtained, beg for this n. KMP Circulation section, two formulas: if n% (n-pre[n] = = 0 indicates the existence of a cyclic section, the number of cycles is N/(N-pre[n])
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <algorithm>5#include <iostream>6#include <cmath>7#include <queue>8#include <map>9#include <stack>Ten#include <list> One#include <vector> A - using namespacestd; - the Const intMAXN =66666666; - intna, NB; - CharA[MAXN]; - CharB[MAXN]; + intPRE[MAXN]; - + //B is the pattern string, a is the target string A voidGetpre (Char*b,int*pre) { at intJ, K; -pre[0] = -1; -j =0; -K =-1; - while(J <nb) { - if(k = =-1|| B[J] = =B[k]) { inJ + +; -k++; toPRE[J] =K; + } - Else { theK =Pre[k]; * } $ }Panax Notoginseng } - the intKMP () { + intAns =0; A inti =0; the intj =0; + Getpre (b, pre); - while(I <na) { $ if(j = =-1|| A[i] = =B[j]) { $i++; -J + +; - } the Else { -j =Pre[j];Wuyi } the if(J = =nb) { -ans++; Wu } - } About returnans; $ } - - intMain () { -Freopen ("inch","R", stdin); A while(~SCANF ("%s", b) && strcmp (".", B)) { +NB =strlen (b); the Getpre (b, pre); - intloop = NB-PRE[NB]; $ if(nb% loop = =0) { theprintf"%d\n", NB/loop); the } the Else { theprintf"1\n"); - } in } the}
[POJ2406] Power Strings