標籤:結構 匹配 模式 div 尾碼 src 位置 ges stat
直接接上篇上代碼:
1 //KMP演算法 2 public class KMP { 3 4 // 擷取next數組的方法,根據給定的字串求 5 public static int[] getNext(String sub) { 6 7 int j = 1, k = 0; 8 int[] next = new int[sub.length()]; 9 next[0] = -1; // 這個是規定10 next[1] = 0; // 這個也是規定11 //12 while (j < sub.length() - 1) {13 if (sub.charAt(j) == sub.charAt(k)) {14 next[j + 1] = k + 1;15 j++;16 k++;17 } else if (k == 0) {18 next[j + 1] = 0;19 j++;20 } else {21 k = next[k];22 }23 24 }25 return next;26 }27 28 // 根據給定的主串和子串,採用KMP演算法來擷取模式比對29 public static int kmp(String src, String sub) {30 31 // 首先產生模式串sub的next[j]32 int[] next = getNext(sub);33 int i = 0, j = 0, index = -1;34 while (i < src.length() && j < sub.length()) {35 if (src.charAt(i) == sub.charAt(j)) {36 i++;37 j++;38 } else if (j == 0) {39 i++;40 } else {41 j = next[j];42 }43 }44 45 // 得到開始匹配的位置索引46 if (j == sub.length()) {47 index = i - sub.length();48 }49 return index;50 }51 }
//
1 //KMP演算法的測試 2 public class Test { 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 String src = "aaaaaaab"; 8 String sub = "ABCDABDABD"; 9 int[] next = KMP.getNext(sub);10 //int[] next = lengthKMP(sub.toCharArray());11 for (int i : next) {12 System.out.print(i + " ");// -1 0 1 2 313 }14 15 // System.out.println();16 // System.out.println(KMP.kmp(src, sub));17 }18 19 //補充上一篇中的對於首碼尾碼的討論的擷取部分匹配數組的演算法20 public static int[] lengthKMP(char[] mchar) {21 int[] fixNum = new int[mchar.length];22 for (int i = 1, j = 0; i < mchar.length; i++) {23 if (mchar[j] == mchar[i]) {24 fixNum[i] = j + 1;25 j++;26 } else if (j > 0) {27 j = 0;28 i -= j;29 }30 }31 // return [0, 0, 0, 0, 1, 2, 0, 1, 2, 0]ABCDABDABD32 return fixNum;33 }34 }
Java資料結構之字串模式比對演算法---KMP演算法2