-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathknuthmorispratt.java
53 lines (43 loc) · 981 Bytes
/
knuthmorispratt.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
package strings;
public class knuthmorispratt {
public static void main(String[] args) {
// TODO Auto-generated method stub
// pitable("aaaa");
pitable("aaaabaacd");
String s = "ababcaababcaabc";
String pat = "ababcaabc";
// kmp(s, pat);
}
public static void kmp(String s, String pat) {
int[] pitable = pitable(pat);
int i = 0, j = 0;
while (i < s.length()) {
while (j >= 0 && s.charAt(i) != pat.charAt(j)) {
j = pitable[j];
}
i++;
j++;
if (j == pat.length()) {
System.out.println("Patern is found at " + (i - j));
j = pitable[j];
}
}
}
public static int[] pitable(String pat) {
int[] pitable = new int[pat.length() + 1];
int i = 0, j = -1;
pitable[0] = -1;
while (i < pat.length()) {
while (j >= 0 && pat.charAt(i) != pat.charAt(j)) {
j = pitable[j];
}
i++;
j++;
pitable[i] = j;
}
for (int k = 0; k < pitable.length; k++) {
System.out.println(pitable[k]);
}
return pitable;
}
}