Simple and KMP pattern matching algorithm (Java)
Simple pattern matching algorithm
Public class Test {// simple mode matching algorithm public int Index (String s, String t, int pos) {int I = pos; // compare the first position of the Main string to int j = 0; // the first position in the mode string while (I
= T. length () {return i-t.length ();} else {return-1 ;}} public static void main (String [] args) {Test test = new Test (); System. out. println (test. index (goodgoogle, google, 0); System. out. println (test. index (goodgoogle, google, 2); System. out. println (test. index (googlegoogle, google, 0); System. out. println (test. index (googlegoogle, google, 2 ));}}
Running result:
4406
KMP pattern matching algorithm
Public class Test {public static void main (String [] args) {Test test = new Test (); test. getNext (abcabx); test. getNext (abcdex); System. out. println (test. indexKMP (goodgoogle, google, 0 ));} //////// KMP mode matching algorithm // public int [] getNext (String T) {int [] next = new int [T. length ()]; int I = 0; int j =-1; next [I] = j; while (I
= T. length () {return i-T.length () ;}else {return 0 ;}}}
Running result:
-100012-100000-1000104