-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMP.java
More file actions
45 lines (41 loc) · 1.33 KB
/
Copy pathKMP.java
File metadata and controls
45 lines (41 loc) · 1.33 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
/*
* Sample usage: java KMP DABRA AABRAACADABRAACAADABRA
*/
public class KMP {
private String pat;
private int[][] dfa;
public KMP(String pat){
// Build DFA from pattern.
this.pat = pat;
int M = pat.length();
int R = 256;
dfa = new int[R][M];
dfa[pat.charAt(0)][0] = 1;
for (int X=0, j=1; j<M; j++){
// Compute dfa[][j].
for (int c=0; c<R; c++)
dfa[c][j] = dfa[c][X]; // Copy mismatch cases
dfa[pat.charAt(j)][j] = j+1; // Set match case
X = dfa[pat.charAt(j)][X]; // Update restart state
}
}
public int search(String txt){
// Simulate operation of DFA on txt.
int i, j, N = txt.length(), M = pat.length();
for (i=0, j=0; i<N && j<M; i++)
j = dfa[txt.charAt(i)][j];
if (j==M) return i-M; // found (hit end of pattern)
else return N; // not found (hit end of text)
}
public static void main(String[] args){
String pat = args[0];
String txt = args[1];
KMP kmp = new KMP(pat);
StdOut.println("text: " + txt);
int offset = kmp.search(txt);
StdOut.print("pattern: ");
for (int i = 0; i<offset; i++)
StdOut.print(" ");
StdOut.println(pat);
}
}