-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuffix_array.java
More file actions
222 lines (184 loc) · 6.64 KB
/
suffix_array.java
File metadata and controls
222 lines (184 loc) · 6.64 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
Suffix Array construction with Longest Common Prefix (LCP) array using Kasai's algorithm.
A suffix array is a sorted array of all suffixes of a string. The LCP array stores the length
of the longest common prefix between consecutive suffixes in the suffix array.
Key operations:
- Constructor: Build suffix array and LCP array for a string
- findPattern(pattern): Find all occurrences of pattern in text
Time complexity: O(n log n) for suffix array, O(n) for LCP array
Space complexity: O(n)
*/
import java.util.*;
class suffix_array {
static class SuffixArray {
private String text;
private int n;
private int[] sa;
private int[] lcp;
SuffixArray(String text) {
this.text = text;
this.n = text.length();
this.sa = buildSuffixArray();
this.lcp = buildLCPArray();
}
private int[] buildSuffixArray() {
Integer[] suffixes = new Integer[n];
for (int i = 0; i < n; i++) {
suffixes[i] = i;
}
Arrays.sort(suffixes, (a, b) -> text.substring(a).compareTo(text.substring(b)));
int[] result = new int[n];
for (int i = 0; i < n; i++) {
result[i] = suffixes[i];
}
return result;
}
private int[] buildLCPArray() {
if (n == 0) return new int[0];
int[] rank = new int[n];
for (int i = 0; i < n; i++) {
rank[sa[i]] = i;
}
int[] lcp = new int[n];
int h = 0;
for (int i = 0; i < n; i++) {
if (rank[i] > 0) {
int j = sa[rank[i] - 1];
while (i + h < n && j + h < n && text.charAt(i + h) == text.charAt(j + h)) {
h++;
}
lcp[rank[i]] = h;
if (h > 0) h--;
}
}
return lcp;
}
List<Integer> findPattern(String pattern) {
if (pattern.isEmpty()) return new ArrayList<>();
int m = pattern.length();
int left = 0, right = n;
while (left < right) {
int mid = (left + right) / 2;
String suffix = text.substring(sa[mid]);
if (suffix.compareTo(pattern) < 0) {
left = mid + 1;
} else {
right = mid;
}
}
int start = left;
left = start;
right = n;
while (left < right) {
int mid = (left + right) / 2;
String suffix = text.substring(sa[mid], Math.min(sa[mid] + m, n));
if (suffix.compareTo(pattern) <= 0) {
left = mid + 1;
} else {
right = mid;
}
}
int end = left;
List<Integer> result = new ArrayList<>();
for (int i = start; i < end; i++) {
if (sa[i] + m <= n && text.substring(sa[i], sa[i] + m).equals(pattern)) {
result.add(sa[i]);
}
}
Collections.sort(result);
return result;
}
int[] getSA() {
return sa;
}
int[] getLCP() {
return lcp;
}
}
static void testMain() {
SuffixArray sa = new SuffixArray("banana");
assert Arrays.equals(sa.getSA(), new int[] {5, 3, 1, 0, 4, 2});
assert Arrays.equals(sa.getLCP(), new int[] {0, 1, 3, 0, 0, 2});
List<Integer> positions = sa.findPattern("ana");
assert positions.equals(Arrays.asList(1, 3));
}
// Don't write tests below during competition.
static void testEmptyString() {
SuffixArray sa = new SuffixArray("");
assert sa.getSA().length == 0;
assert sa.getLCP().length == 0;
assert sa.findPattern("a").isEmpty();
}
static void testSingleChar() {
SuffixArray sa = new SuffixArray("a");
assert sa.getSA().length == 1;
assert sa.getSA()[0] == 0;
}
static void testRepeatedChars() {
SuffixArray sa = new SuffixArray("aaaa");
assert Arrays.equals(sa.getSA(), new int[] {3, 2, 1, 0});
assert Arrays.equals(sa.getLCP(), new int[] {0, 1, 2, 3});
}
static void testPatternNotFound() {
SuffixArray sa = new SuffixArray("hello");
assert sa.findPattern("world").isEmpty();
}
static void testPatternAtEnd() {
SuffixArray sa = new SuffixArray("hello");
assert sa.findPattern("lo").equals(Arrays.asList(3));
}
static void testOverlappingPatterns() {
SuffixArray sa = new SuffixArray("aabaabaa");
List<Integer> positions = sa.findPattern("aa");
Collections.sort(positions);
assert positions.equals(Arrays.asList(0, 3, 6));
}
static void testEntireString() {
String text = "programming";
SuffixArray sa = new SuffixArray(text);
assert sa.findPattern(text).equals(Arrays.asList(0));
}
static void testLCPCalculation() {
SuffixArray sa = new SuffixArray("abcab");
assert Arrays.equals(sa.getSA(), new int[] {3, 0, 4, 1, 2});
assert sa.getLCP()[0] == 0;
assert sa.getLCP()[1] == 2;
assert sa.getLCP()[2] == 0;
assert sa.getLCP()[3] == 1;
assert sa.getLCP()[4] == 0;
}
static void testAllUniqueChars() {
SuffixArray sa = new SuffixArray("abcd");
assert Arrays.equals(sa.getSA(), new int[] {0, 1, 2, 3});
assert Arrays.equals(sa.getLCP(), new int[] {0, 0, 0, 0});
}
static void testPalindrome() {
SuffixArray sa = new SuffixArray("racecar");
List<Integer> positions = sa.findPattern("r");
Collections.sort(positions);
assert positions.equals(Arrays.asList(0, 6));
}
static void testLongPattern() {
String text = "thequickbrownfoxjumpsoverthelazydog";
SuffixArray sa = new SuffixArray(text);
assert sa.findPattern("jumps").equals(Arrays.asList(16));
List<Integer> thePos = sa.findPattern("the");
Collections.sort(thePos);
assert thePos.equals(Arrays.asList(0, 25));
}
public static void main(String[] args) {
testMain();
testEmptyString();
testSingleChar();
testRepeatedChars();
testPatternNotFound();
testPatternAtEnd();
testOverlappingPatterns();
testEntireString();
testLCPCalculation();
testAllUniqueChars();
testPalindrome();
testLongPattern();
System.out.println("All tests passed!");
}
}