KMP algorithm, I feel very well understood when I start school. But I really want to make the code of the realization, spend a lot of energy. This is the ultimate implementation code:
#include
Void get_next {for (int W = 0; W <128; W ) Next [w] = 0xEfffffff; INT i = 1, j = -1; Next [0] = -1;
INT Plen = Strlen (P); While (i INDEX_KMP (CHAR * S, CHAR * P, INT POS) {INT I, J; INT SLEN = Strlen (s); int Plen = Strlen (P); i = POS; J = -1; While (i INT main (void) {char strs [] = "aabcab"; char strp [] = "c"; GET_NEXT (STRP); INT NPOS = index_kmp (strs, strp, 0); cout << "the position is: / t" << npos << endl; return 0;} Understanding this algorithm has taken a few days of spare time. The Yin version of the data structure book is subject to an object-oriented, so there is some cases of the algorithm. The algorithm on the "Software Designer Tutorial" of the Computer Level and Qualification Exam seems to have a mistake, that is, I logo in the code. If the J's initial value is 0, there will be errors for a single character string. From the information on the online search, many people can't understand the text [] constructor. This is indeed the difficulties and key points of this algorithm. In fact, the Yin version of the data structure is quite about this, if you can explain this Look at 10 times seriously (I feel that I have seen more than 10 times before and after, I don't think about it. I can learn this.), Then push those results 2 times (I am on paper I wrote it again. In fact, every time I see it, I should be able to understand it. I feel that the key point is that the idea is a method similar to inductive method. Suppose Next [J] = K, this is true when j == 0 (at this time k = -1), then it is then launched next to NEXT [J 1]. It is quite a thing to write.