-
Notifications
You must be signed in to change notification settings - Fork 13
/
KMP.java
68 lines (59 loc) · 1.58 KB
/
KMP.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package algorithm.note;
/**
* @author: mayuan
* @desc: kmp 算法实现 https://www.cnblogs.com/yjiyjige/p/3263858.html
* @date: 2018/12/25
*/
public class KMP {
public static void main(String[] args) {
final String ts = "abcdabde";
final String ps = "abd";
System.out.println(kmp(ts, ps));
System.out.println(ts.indexOf(ps));
}
public static int kmp(String ts, String ps) {
char[] t = ts.toCharArray();
char[] p = ps.toCharArray();
// 主串的位置
int i = 0;
// 模式串的位置
int j = 0;
int[] next = KMP.getNext(ps);
while (i < t.length && j < p.length) {
// 当j为-1时,要移动的是i,当然j也要归0
if (-1 == j || t[i] == p[j]) {
++i;
++j;
} else {
// i不需要回溯,j回到指定位置.
j = next[j];
}
}
// 返回匹配的起始索引位置
if (j == p.length) {
return i - j;
} else {
return -1;
}
}
/**
* 获取 next 数组
*
* @param pattern 模式串
* @return
*/
public static int[] getNext(String pattern) {
char[] p = pattern.toCharArray();
int[] next = new int[p.length];
next[0] = -1;
int k = -1, j = 0;
while (j < p.length - 1) {
if (-1 == k || p[k] == p[j]) {
next[++j] = ++k;
} else {
k = next[k];
}
}
return next;
}
}