KMP algorithm Learning

Source: Internet
Author: User

(1) Reference website:

KMP algorithm: http://www.cnblogs.com/dolphin0520/archive/2011/08/24/2151846.html

--- Solve the next Array Using recursive and direct methods

KMP Learning Experience: http://www.java3z.com/cwbwebhome/article/article19/res023.html? Id = 3737

--- When a string starts from 0, next [I] can be described as "not the maximum length of the first and last compound substring ".

KMP string pattern matching in Pattern Function: http://www.java3z.com/cwbwebhome/article/article19/re022.html? Id = 3731

--- Describes in detail how to solve the next array. [improved version of next array solution]

 

(2) classic summary:

I. How to calculate the string mode Value next [N]
Definition:
(1) next [0] =-1 meaning: the mode Value of the first character of any string must be-1.
(2) next [J] =-1 meaning: The character marked as J in the mode string T. If the character is the same as the first character, and 1 ~ K characters and 1 ~ K characters are not equal to or equal
T [k] = T [J] (1 ≤ k <j ). For example, t = "abcabcad", next [6] =-1, because T [3] = T [6] BCA

(3) next [J] = K meaning: The character marked as J in the mode string T. If the first k characters of J are the same as the first k characters, and t [J]! = T [k] (1 ≤ k <j ).
That is, t [0] T [1] T [2]... T [k-1] = T [J-K] T [J-k + 1] T [J-K + 2]… T [J-1] and T [J]! = T [K]. (1 ≤ k <j );
(4) next [J] = 0 meaning: Except for (1) (2) (3.

 

Ii. What is the meaning of the next function value?

Locate the pattern string t in string s, if s [m]! = T [N], then, take the pattern function value of T [N] Next [N],
1. next [N] =-1 indicates that s [m] and T [0] are indirectly compared. They are not equal. Next time we compare s [M + 1] and T [0]
2. Next [N] = 0 indicates that the comparison process produces an inequality. The next comparison is s [m] and T [0].
3. next [N] = k> 0 but k <n, indicating that the first k characters of S [m] are indirectly equal to the first k characters in T, is the next comparison of S [m] and T [k] equal?
4. other values, not possible.

(3) code implementation [reference book: "data structure and algorithm Java language description" by Deng Junhui]

 

Original book code: [slightly changed]

Package DSA;/** string mode matching: KMP algorithm */public class pm_kmp {public static void main (string [] ARGs) {system. out. println (PM ("ababacab", "ACA");} public static int PM (string T, string P) {// KMP algorithm int [] Next = buildnextimproved (p); // construct the next [] Table int I = 0; // The master string pointer Int J = 0; // pattern string pointer while (j <p. length () & I <t. length () {// compare the characters showprogress (T, P, I-j, j) one by one from left to right; shownexttable (next, I-j, P. length (); system. out. P Rintln (); If (0> j | T. charat (I) = P. charat (j) {// If the match exists, or P has been removed from the leftmost side (question: can the two conditions exchange order ?) I ++; j ++; // then go to the next pair of characters} else // otherwise J = next [J]; // The mode string is shifted right (note: do not roll back the main string)} // whilereturn (I-j);} protected static int [] buildnext (string P) {// create the next [] Table int [] Next = new int [p. length ()]; // next [] Table Int J = 0; // "master" string pointer int T = next [0] =-1; // "Mode" string pointer while (j <p. length ()-1) if (0> T | P. charat (j) = P. charat (t) {// match j ++; t ++; next [J] = T; // This sentence can be improved ...} else // mismatch T = next [T]; for (j = 0; j <p. length (); j ++) S Ystem. out. print ("\ t" + P. charat (j); system. out. print ("\ n"); shownexttable (next, 0, P. length (); Return (next);} protected static int [] buildnextimproved (string p) {// create the next [] Table of the mode string P (Improved Version) int [] Next = new int [p. length ()]; // next [] Table Int J = 0; // "master" string pointer int T = next [0] =-1; // "Mode" string pointer while (j <p. length ()-1) if (0> T | P. charat (j) = P. charat (t) {// match j ++; t ++; next [J] = (P. charat (j )! = P. charat (t ))? T: Next [T]; // note the difference between this sentence and the previous sentence without improvement} else // mismatch T = next [T]; for (j = 0; j <p. length (); j ++) system. out. print ("\ t" + P. charat (j); system. out. print ("\ n"); shownexttable (next, 0, P. length (); Return (next);} protected static void shownexttable (INT [] n, int offset, int length) {// display the next [] table, int I; for (I = 0; I <OFFSET; I ++) system. out. print ("\ t"); for (I = 0; I <length; I ++) system. out. print ("\ t" + N [I]); system. out. print ("\ n");} protected static void showprogress (// dynamic display of matching progress string T, // Main string P, // mode string int I, // start position of the mode string relative to the master string Int J) // The current character {int t; system. out. println ("-----------------------------------------"); For (t = 0; t <t. length (); t ++) system. out. print ("\ t" + T. charat (t); system. out. print ("\ n"); If (0 <= I + J) {for (t = 0; t <I + J; t ++) system. out. print ("\ t"); system. out. print ("\ t |");} system. out. println (); For (t = 0; t <I; t ++) system. out. print ("\ t"); For (t = 0; t <p. length (); t ++) system. out. print ("\ t" + P. charat (t); system. out. print ("\ n"); system. out. println ();}}

 

Simplified code:

 

Package DSA. me; public class KMP {public static void main (string [] ARGs) {system. out. println (KMP ("Ababa", "Bab"); system. out. println (KMP ("Ababa", "BBA");} // KMP algorithm public static int KMP (string T, string P) {int next [] = buildenextimproved (p); int I = 0; Int J = 0; while (I <t. length () & J <p. length () {// they must be within the range. Otherwise, if (j <0 | T. charat (I) = P. charat (j) {// match, I and j move to the right, j <0 (j =-1) I ++; j ++;} els E {// mismatch. As long as J is moved, I do not backtrack J = next [J];} If (j> = P. length () Return (I-j); elsereturn-1;} // evaluate the next array public static int [] buildenext (string P) {int [] Next = new int [p. length ()]; Int J = 0; int t =-1; // The initial value is-1 next [0] =-1; while (j <p. length ()-1) {If (t =-1 | P. charat (t) = P. charat (j) {// match --- if the same string at the beginning and end does not exist in J, next [J] will be equal to 0 T ++; j ++; next [J] = T; // we can see that J in the while condition cannot be equal to P. length ()-1} else {// mismatch T = next [T] ;}} return next ;}// evaluate the improved version of the next array public static int [] buildenextimproved (string p) {int [] Next = new int [p. length ()]; Int J = 0; int t =-1; // The initial value is-1 next [0] =-1; while (j <p. length ()-1) {If (t =-1 | P. charat (t) = P. charat (j) {// match --- if the same string at the beginning and end does not exist in J, next [J] will be equal to 0 T ++; j ++; next [J] = (P. charat (j )! = P. charat (t ))? T: Next [T]; // improvement} else {// mismatch T = next [T] ;}} return next ;}}

 

Attached to the relevant chapter of the Reference Books PDF: http://115.com/file/dpkg6art?data structure and algorithm (java _###

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.