-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMP.java
More file actions
34 lines (32 loc) · 1.07 KB
/
KMP.java
File metadata and controls
34 lines (32 loc) · 1.07 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
// Jingze Dai, 400201059, daij24
public class KMP {
private static int[] longest_suffix_prefix(String A, String B){
String P = B + A;
int i = 0;
int k = -1;
int m = P.length();
int[] next = new int[m + 1];
next[0] = -1;
while (i < m) {
while (k >= 0 && P.charAt(k) != P.charAt(i)){
k = next[k];
}
i = i + 1;
k = k + 1;
next[i] = k;
}
return next;
}
public static void main(String[] args){
String A = "aabbccdd";
String B = "bbccddaaddee";
int[] output = longest_suffix_prefix(A, B);
System.out.println("Matched Longest Suffix with Prefix: ");
if (output[A.length() + B.length()] > 0) {
System.out.println(B.substring(0, output[A.length() + B.length()]));
System.out.println("Length: " + output[A.length() + B.length()]);
} else {
System.out.println("This is an empty string (with length 0)");
}
}
}