Evaluate the next [] of KMP-with the next [] source code starting with a traditional 0 subscript-The KMP algorithm is executed based on the value of the next [] in a known mode string.
-The value of next [] depends only on the mode string itself ------------------------------------------------------------
Definition of next:
Next [J] =
= 0 when j = 1
= Max {k | 1 <k <j and 'P (1 )... P (k-1) '= 'P (J-k + 1 )... P (J-1) '} when this set is not empty
= 1 Other Cases
-------------------------------------------------------------
The following is an example of the above definition.
With:
--------------------------------
J: 1 2 3 4 5 6 7 8
String: A B C A C
--------------------------------
Find: Next [J] =?
First, next [1] = 0
--------------------------------
J: 1 2 3 4 5 6 7 8
String: A B C A C
Next [J]: 0
--------------------------------
Next [J + 1] =? -- (J = 1)
1 <k <j, j = 1,
So, next [2] = 1
--------------------------------
J: 1 2 3 4 5 6 7 8
String: A B C A C
Next [J]: 0 1
--------------------------------
Next [3] = next [J + 1]? -- (J = 2)
Because: Next [2] = 1, again, P (1 )! = P (2), and next [1] = 0
So next [3] = 1
--------------------------------
J: 1 2 3 4 5 6 7 8
String: A B C A C
Next [J]: 0 1 1
--------------------------------
Next [4] =?
Because next [3] = 1, P (3) = P (1)
So next [4] = next [3] + 1 = 2
--------------------------------
J: 1 2 3 4 5 6 7 8
String: A B C A C
Next [J]: 0 1 1 2
--------------------------------
Next [5] =?
Next [4] = 2 => P (4 )! = P (2 ),
Next [2] = 1 => P (4) = P (1 ),
Next [5] = next [2] + 1 = 2
--------------------------------
J: 1 2 3 4 5 6 7 8
String: A B C A C
Next [J]: 0 1 1 2 2
--------------------------------
Next [6] =?
Next [5] = 2 => P (5) = P (2)
Next [6] = next [5] + 1 = 3
--------------------------------
J: 1 2 3 4 5 6 7 8
String: A B C A C
Next [J]: 0 1 1 2 2 3
--------------------------------
Next [7] =?
Next [6] = 3 ==> P (6 )! = P (3 ),
Next [3] = 1 ==> P (6 )! = P (1 ),
Next [1] = 0
So, next [7] = 1
--------------------------------
J: 1 2 3 4 5 6 7 8
String: A B C A C
Next [J]: 0 1 1 2 2 3 1
--------------------------------
Next [8] =?
Next [7] = 1 ==> P (7) = P (1)
So, next [8] = next [7] + 1 = 2
--------------------------------
J: 1 2 3 4 5 6 7 8
String: A B C A C
Next [J]: 0 1 1 2 2 3 1 2
--------------------------------####################################### ###########
######################################## ##########
As you can see, after determining the next [] = 0,
The rest can be recursive in sequence.
This algorithm is available,
However, this is slightly different from the above.
The reason is: in C, the traditional array subscript starts from 0,
Therefore, you must:
5
6 int *
7 get_next (const char * const Str)
8 {
9 int * Next, Len = strlen (STR), I, J;
10 assert (null! = (Next = (int *) malloc (sizeof (INT) * (LEN ))));
11 For (next [0] =-1, I = 1, j =-1; I <Len; ++ I ){
12 For (j = I-1; J! =-1 & STR [J]! = STR [next [J]; j = next [J])
13;
14 next [I] = (J! =-1 & STR [J] = STR [next [J])? Next [J] + 1:0;
15}
16 return next;
17}
18 int
19 main (INT argc, char ** argv)
20 {
21 const char a [] = "abcabaa ";
22 const char B [] = "abcabaabcaabaabcabaabcabaabaabcaabaabaabaabacacabaa ";
23 int * next1 = get_next ();
24 int I, J;
25 For (I = 0; I <strlen (a); ++ I ){
26 printf ("% d/T", next1 [I]);
27}
28 putchar ('/N ');
29 for (I = 0, j = 0; I <strlen (B );){
30 if (j = strlen ()){
31 printf ("% d/N", I-strlen ());
32 J = 0;
33}
34 J = (-1 = J )? (++ I, 0): A [J] = B [I]? (++ I, j + 1): next1 [J];
35}
36 exit (0 );
37}
The running result of the above program is:
-1 0 0 0 1 2 0
0
13 The first line shows the result of next []. As you can see, I have no free (next), but after exit, the system will do it ,:))
Rows 2nd and 3rd indicate the starting position of the source string that matches the pattern string. PS: This is just a simple note, so that you can remember and apply it easily, in a few days, I will write a complete KMP for you.
If something is wrong with my understanding, I hope you can point out that this dd is a bit dizzy from the morning, and it will be just a bit sleepy * ^_^ *
Next, based on nextval [] 18 int *
19 get_nextval (const char * const STR, int * const next)
20 {
21 int I, j, Len = strlen (STR );
22 For (I = 1; I <Len; ++ I ){
23/* j = next [I]; */
24 if (j = next [I])! = 0 & STR [I] = STR [J])
25 next [I] = next [J];
26}
27 return next;
28} PS: I used this pattern matching. No problem occurred. The Code should be correct,
Because next [] is rarely used, the improved nextval [] is better.
It is the section I wrote in the program: 7/****************** static functions *****************/
8 Static int *
9 _ get_nextval (const char * const t_str)
10 {
11 int * Next, Len = strlen (t_str), I, J;
12 assert (null! = (Next = (int *) malloc (sizeof (INT) * (LEN ))));
13
14 For (next [0] =-1, I = 1, j =-1; I <Len; ++ I ){
15 For (j = I-1; J! =-1 & t_str [J]! = T_str [next [J]; j = next [J]);
16 next [I] = (J! =-1 & t_str [J] = t_str [next [J])? Next [J] + 1:0;
17}
18 For (I = 1; I <Len; ++ I ){
19 if (j = next [I])! = 0 & t_str [I] = t_str [J])
20 next [I] = next [J];
21}
22 return next;
23}