-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
LCPArray.java
78 lines (64 loc) · 2.66 KB
/
LCPArray.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.jwetherell.algorithms.data_structures;
import java.util.ArrayList;
/**
* In computer science, the longest common prefix array (LCP array) is an auxiliary
* data structure to the suffix array. It stores the lengths of the longest common
* prefixes (LCPs) between all pairs of consecutive suffixes in a sorted suffix array.
* <p>
* @see <a href="https://en.wikipedia.org/wiki/LCP_array">LCP Array (Wikipedia)</a>
* <br>
* @author Jakub Szarawarski <[email protected]>
* @author Justin Wetherell <[email protected]>
*/
public class LCPArray<C extends CharSequence> {
private static final char DEFAULT_END_SEQ_CHAR = '$';
private final char endSeqChar;
private SuffixArray suffixArray;
private ArrayList<Integer> lcp;
public LCPArray(C sequence){
this(sequence, DEFAULT_END_SEQ_CHAR);
}
public LCPArray(C sequence, char endChar) {
endSeqChar = endChar;
suffixArray = new SuffixArray(sequence, endSeqChar);
}
public ArrayList<Integer> getLCPArray() {
if (lcp == null)
LCPAlgorithm();
return lcp;
}
private void LCPAlgorithm() {
final ArrayList<Integer> LCPR = getLCPR();
getLCPfromLCPR(LCPR);
}
private ArrayList<Integer> getLCPR() {
final ArrayList<Integer> KMRArrayList = suffixArray.getKMRarray();
final ArrayList<Integer> suffixArrayList = suffixArray.getSuffixArray();
final String string = suffixArray.getString();
final int length = KMRArrayList.size();
final ArrayList<Integer> LCPR = new ArrayList<Integer>(); // helper array, LCP[i] = LCPR[suffixArray[i]]
int startingValue = 0;
for (int i=0; i<length; i++) {
if(KMRArrayList.get(i).equals(0)) {
LCPR.add(0);
startingValue = 0;
} else {
int LCPRValue = startingValue;
final int predecessor = suffixArrayList.get(KMRArrayList.get(i)-1);
while (string.charAt(i+LCPRValue) == string.charAt(predecessor+LCPRValue))
LCPRValue++;
LCPR.add(LCPRValue);
startingValue = LCPRValue-1 > 0 ? LCPRValue-1 : 0;
}
}
return LCPR;
}
private void getLCPfromLCPR(ArrayList<Integer> LCPR) {
final ArrayList<Integer> suffixArrayList = suffixArray.getSuffixArray();
final int length = suffixArrayList.size();
lcp = new ArrayList<Integer>();
lcp.add(null); //no value for LCP[0]
for (int i=1; i<length; i++)
lcp.add(LCPR.get(suffixArrayList.get(i)));
}
}