Link:
http://poj.org/problem?id=2406
Power StringsTime
limit:3000MS
Memory Limit:65536KB
64bit IO Format:%i64d &%i64 U SubmitStatus
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. Code:
#include <stdio.h>#include<string.h>#include<stdlib.h>#defineN 1000007CharS[n];intNext[n]; ///Next, the maximum similarity between prefix and suffix is saved.voidFindNext (intSlen,intNext[])///Next[i] represents the maximum match of the first I character{ intI=0, j=-1; next[0] = -1; while(i<Slen) { if(j==-1|| s[i]==R[j]) next[++i] = + +J; ElseJ=Next[j]; }}intMain () { while(SCANF ("%s", s), strcmp (S,".")) { intslen=strlen (S); FindNext (Slen, Next); if(slen% (slen-Next[slen])) printf ("1\n"); Elseprintf ("%d\n", slen/(slen-Next[slen])); } return 0;}
View Code
(Find the number of circulation sections) Power Strings--POJ--2406