Topic Link:
poj:http://poj.org/problem?id=1961
hdu:http://acm.hdu.edu.cn/showproblem.php?pid=1358
zoj:http://acm.zju.edu.cn/onlinejudge/showproblem.do?problemcode=2177
The main effect of the topic:
Given a string s of length n, the shortest link for each prefix is obtained. In other words, for each i (2<=i<=n), a maximum integer k>1 (if k exists) is obtained, which makes the prefix of the first I character of S a string repeated K-times. Outputs all the I and corresponding k that exist K.
For example, for string Aabaabaabaab, only K exists when i=2,6,9,12, and is 2,2,3,4
Analysis and Summary:
Very classic A topic, each big OJ have included.
When I started this problem, just read KMP, did not see the shortest circulation section of the relevant information, their own mess, took two discrete math class to think, the result is finally AC = =, but unfortunately, the results found just again HDU and Zoj over, and Poj or wa ...
Then learn the "Introduction Classic algorithm-Training guide" above the method, on the POJ to successfully AC.
Although did not succeed in the Poj AC, but after this question ponder, to kmp the mismatch function obtains next the process and the principle to have the deeper understanding and the realization.
Code:
1. The code of his own disorderly, Poj can't ac, beg break
#include <cstdio> #include <cstring> const int MAXN = 1000005;
Char T[MAXN];
int F[MAXN];
int n;
void Getfail (char *p, int *f) {f[0]=f[1]=0;
int start=1;
int N=strlen (P);
BOOL Flag=true;
for (int i=1; i<n; ++i) {int j=f[i];
while (J && P[i]!=p[j]) {flag=false;
J=F[J];
} if (P[i]==p[j]) {f[i+1]=j+1;
if (f[i+1]==1) {flag=true;
start=i; } if (flag && (i+1) >=start*2 && (i+1)%start==0) {printf ("%d%d\n", i+1, (i
+1)/start);
}} else{flag=true;
start=i+1;
f[i+1]=0;
int main () {int cas=1;
Char ch;
while (~SCANF ("%d%*c", &n) && N) {printf ("Test case #%d\n", cas++); Gets (T);
Getfail (T,F);
Puts ("");
return 0; }