-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMPSample.java
More file actions
42 lines (32 loc) · 862 Bytes
/
KMPSample.java
File metadata and controls
42 lines (32 loc) · 862 Bytes
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
import java.util.Arrays;
public class KMPSample {
public static void main(String[] args){
String word = "rjtsdlrjtsdlrlllll";
int[] table = new int[word.length()];
partial_table(word.toCharArray(), table, word.length());
System.out.println(Arrays.toString(table));
}
public static void partial_table(char[] word, int[] table, int size)
{
if (0 == size)
return;
if (1 == size)
{
table[0] = -1;
return;
}
table[0] = -1;
table[1] = 0;
int pos = 2;
int cnd = 0;
while(pos < size)
{
if (word[pos - 1] == word[cnd])
table[pos++] = ++cnd;
else if (cnd > 0)
cnd = table[cnd];
else
table[pos++] = 0;
}
}
}