About KMP algorithm

 

KMP algorithm:

The nature of this algorithm is first performed for the template string, to generate an array (next array), the array reflects the situation of the template string.

Example:

S:  ABADACABABCD

P: BAB

When we queried P3 and S3 (B and D) are not equal, if the algorithm is violence, then start comparing the second character.

And KMP algorithm is started from the comparison P3 ( P3 where only based on the above cited example, the resulting array is next to the failure to find if every position P3, i.e.

Mismatch, the median pattern string is moving to the right: the position where the character mismatch - next value corresponding to the character mismatch )

Through the array to find again, in order to achieve the objective of reducing the time complexity.

 

next array:

  

           

{Relationship next to the maximum array length value: next array corresponds to the "maximum length" a whole move to the right, then the initial value assigned -1. }

 

 This article is in understanding the specific review of the KMP algorithm, if you click on the link below to see Comments (explain very clearly):

Original link: https: //blog.csdn.net/v_july_v/article/details/7041827

The following are specific examples of realization of java:

 

 1 public class kmp {
 2     
 3     private String ts;
 4     private String ps;
 5     kmp(){
 6     }
 7     kmp(String ts,String ps){
 8         this.ts=ts;
 9         this.ps=ps;
10     }
11     public void setts(String ts){
12         this.ts=ts;
13     }
14     public void setps(String ps){
15         this.ps=ps;
16     }
17     public int kmpmatch() {                    //KMP开始运算
18         int slen=ts.length();
19         int plen=ps.length();
20         int i=0;
21         int j=0;
22         int[] next =getNext(ps);
23         while(i<slen&&j<plen) {
24             if(j==-1||ts.charAt(i)==ps.charAt(j)) {
25                 i++;
26                 j++;
27             }else {
28                 j=next[j];
29             }
30         }
31         if(j==plen) {
32             return i-j;
33         }else {
34             return -1;
35         }
36         
37     }
38     public int[] getNext(String ps) {
39     char[] p =ps.toCharArray();
40     int[] next = new int[p.length];
41     Next [0] = -. 1 ;
 42 is      int J = 0 ;
 43 is      int K = -1 ;
 44 is      the while (J <-p.length. 1) {                     // the essence of the algorithm! ! ! ! ! 
45          IF (K == -. 1 || P [J] == P [K]) {
 46 is              K ++ ;
 47              J ++ ;
 48              IF (P [J] =! P [K]) {
 49                  Next [J] = K ;
 50              } the else {
 51 is                  Next [J] = Next [K];
 52 is              }
 53 is          }else {
54             k=next[k];
55         }
56     }
57         return next;
58     }
59 
60 
61 
62     public static void main(String[] args) {
63         // TODO Auto-generated method stub
64         String A = "wdnmwdnmwdnmddnm";
65         String B = "wdnmwdnmd";
66         kmp k =new kmp();
67         k.setts(A);
68         k.setps(B);
69         System.out.println (k.kmpmatch ());         // output 4 
70      }
 71 is  
72 }

 

Guess you like

Origin www.cnblogs.com/husai/p/12539622.html