KMP algorithm, kmp

Source: Internet
Author: User

KMP algorithm, kmp

The KMP algorithm is the most classic algorithm in string mode matching. It was originally explained in the data structure of the sophomore year, but only remembered the principle, but did not know the code implementation, today, KMP code implementation is finally completed.
Principle
The principle of KMP is actually very simple. Given a string and a mode string, find the position of the mode string in the given string. Convert the two strings into character arrays, and start matching from the starting position "I" and "j" of the two arrays. If the two strings are the same, execute "I ++ ", "j ++" and then compare the next digit. If they are different, go to the next [j] Location corresponding to the next array of the mode string, and start to compare it with the current position of the given string "I, in other words, the pattern string is prefixed by the "j-next [j]" bit to continue the comparison, so that it does not return to the start position for matching every time the occurrence of a mismatch occurs, make full use of the matched locations.

Code
The key to the KMP algorithm is to obtain the next array of the mode string:

Public static int [] next (char [] p) {int len = p. length; int [] next = new int [len]; next [0] = 0; next [1] = 0; // first assign values to next [0] And next [1]. These two numbers are fixed for (int I = 2; I <len; I ++) {int k = next [I-1]; // use an integer number to traverse while (k> = 0) {if (p [I-1] = p [k]) {next [I] = k + 1; // obtain the next value of the current position when matching the character, and then end the loop break;} k -- ;}} return next ;}

After obtaining the next array, you can perform KMP matching:

Public int kmpSearch (char [] s, char [] p) {int I = 0, j = 0; // int slen = s starting from 0. length, plen = p. length; int [] next = next (p); while (I <slen & j <plen) {if (s [I] = p [j]) {// match I ++; j ++;} else {j = next [j]; // if they are not equal, the next [j] position is returned to continue backward matching, no need to compare with the previous one} if (j = plen) // if the match ends, the match is successful and return the start position of the successful match and return I-j; return-1; // otherwise, the matching fails.-1} is returned}

The KMP algorithm also provides an advanced next algorithm to calculate the nextval array:

public int[] nextVal(char[] p) {  int len = p.length;  int[] nextval = new int[len];  nextval[0] = -1;  int i=-1, j = 0;  while(j < len - 1) {  if(i == -1 || p[j] == p[i]) {    ++i;    ++j;    if(p[j] != p[i])      nextval[j] = i;    else      nextval[j] = nextval[i];  }else       i = nextval[i];  }  return nextval;}

 

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.