Home > Back-end >  A subroutine circulation problems (KMP algorithm)
A subroutine circulation problems (KMP algorithm)

Time:03-05

Problems in KMPIndex function while loop,
I input test data is:
Abbabcjhabbabbcd
Abbabb
Don't know why, when j into 1 automatically jump out of the while loop body, lead to the result error, and I was just strlen (I and j) into a plastic variables, program can run successfully,
This is part of the KMPIndex function changes:
int i,j;
I=0, j=0;
Int tlen=strlen (t), slen=strlen (s);
While (iIf (j==1 | | s [I] t [j])=={
i++;
j++;
}
The else {
J=next [j];
}
}
This is a function of change before:
Void getnext (char t [], int next []) {
Int j, k;
J=0;
K=1;
Next [0]=1;
Int d=strlen (t);
While (jIf (k==1 | | t [j]==t [k]) {
j++;
k++;
Next [j]=k;
}
The else {
K=next [k].
}
}
}
This is the change before the entire function:
# include & lt; Stdio. H>
# include & lt; String. H>
Void getnext (char t [], int next []) {
Int j, k;
J=0;
K=1;
Next [0]=1;
Int d=strlen (t);
While (jIf (k==1 | | t [j]==t [k]) {
j++;
k++;
Next [j]=k;
}
The else {
K=next [k].
}
}
}
Void KMPIndex (char t [], char [] s, int next []) {
int i,j;
I=0, j=0;
While (iIf (j==1 | | s [I] t [j])=={
i++;
j++;
}
The else {
J=next [j];
}
}
If (j>=strlen (t)) {
Printf (" % d ", I - strlen (t));
}
The else printf (" ERROR ");
}
Int main () {
Char t [101], [1001] s.
Int next [100];
The scanf (" % s % s ", s, t);
Getnext (t, next);
KMPIndex (t, s, next);
return 0;
}
  • Related