forked from pxu/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMP.java
More file actions
66 lines (49 loc) · 1.17 KB
/
KMP.java
File metadata and controls
66 lines (49 loc) · 1.17 KB
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
import java.util.ArrayList;
public class KMP {
public static int[] next = null;
public static void prt(Object o) {
System.out.print(o);
}
public static void pln(Object o) {
System.out.println(o);
}
public static void buildNext(String t) {
int n = t.length();
next = new int[n];
if(n < 2) return;
next[0] = next[1] = 0;
for(int i=2; i<=n-1; ++i) {
int j = next[i-1];
while(j != 0 && t.charAt(j) != t.charAt(i-1)) j = next[j];
if(t.charAt(j) == t.charAt(i-1)) next[i] = j+1;
else next[i] = 0;
}
}
public static ArrayList<Integer> find(String s, String t) {
int ns = s.length(), nt = t.length();
ArrayList<Integer> res = new ArrayList<Integer> ();
if(ns < nt) return res;
int ps = 0, pt = 0;
for(; ps < ns; ++ps) {
while(pt != 0 && s.charAt(ps) != t.charAt(pt)) {
pt = next[pt];
}
if(s.charAt(ps) == t.charAt(pt)) {
++pt;
}
if(pt == nt) {
res.add(ps-nt+1);
pt = 0;
}
}
return res;
}
public static void main(String[] args) {
String s = "i love you i love you ";
String t = "love";
buildNext(t);
ArrayList<Integer> res = new ArrayList<Integer> ();
res = find(s, t);
pln(res);
}
}