KMP algorithm - Popular ETS

public  class KMPAlgorithm {
     public  static  void main (String [] args) { 
        String str1 = "ABCDAB ABCDABCDABDE the BBC" ; 
        String str2 = "ABCDABD" ;
         int [] = kmpTable kmpTable (str2); 
        System.out.println (of Arrays.toString (kmpTable)); 
    } 

    public  static  int [] kmpTable (String dest) { 

        int Next [] = new new  int [dest.length ()]; 
        Next [ 0] = 0; // if the character length is 1, the portion matching value is 0 
        for ( int i = 0, j = 1; j < dest.length(); j++) {            //ABCDABD

            while (i > 0 && dest.charAt(i) != dest.charAt(j)) {
                i = next[i - 1];
            }

            if (dest.charAt(i) == dest.charAt(j)) { //
                i++;
            }

            next[j] = i;
        }

        return next;
    }
}

Guess you like

Origin www.cnblogs.com/MrRightZhao/p/12116915.html
ets