您的位置:首页 > 理论基础 > 数据结构算法

Java数据结构之字符串模式匹配算法---KMP算法2

2016-11-13 20:50 453 查看


直接接上篇上代码:

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 3
13         }
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]ABCDABDABD
32         return fixNum;
33     }
34 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: