forked from destiny1020/algorithm_playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMP.java
More file actions
59 lines (51 loc) · 1.01 KB
/
KMP.java
File metadata and controls
59 lines (51 loc) · 1.01 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
package chap5;
public class KMP
{
private String pattern;
private int[][] dfa;
public static int R = 256;
public KMP(String pattern)
{
this.pattern = pattern;
this.initDFA();
}
private void initDFA()
{
int M = this.pattern.length();
this.dfa = new int[R][M];
// Route leads to State 1
this.dfa[this.pattern.charAt(0)][0] = 1;
for(int X = 0, j = 1; j < M; j++)
{
// Set Mismatch
for(int c = 0; c < R; c++)
{
this.dfa[c][j] = this.dfa[c][X];
}
// Set Match
this.dfa[this.pattern.charAt(j)][j] = j + 1;
// Upgrade the restart state
X = this.dfa[this.pattern.charAt(j)][X];
}
}
public int process(String txt)
{
int M = this.pattern.length();
int N = txt.length();
int i, j;
for(i = 0, j = 0; j < M && i < N; i++)
{
j = this.dfa[txt.charAt(i)][j];
}
if(j == M)
return i - M;
else
return -1;
}
public static void main(String[] args)
{
KMP kmp = new KMP("ABRA");
int result = kmp.process("ABACADABRAC");
System.out.println(result);
}
}