SuffixArrayX.java
10.6 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/******************************************************************************
* Compilation: javac SuffixArrayX.java
* Execution: java SuffixArrayX < input.txt
* Dependencies: StdIn.java StdOut.java
* Data files: http://algs4.cs.princeton.edu/63suffix/abra.txt
*
* A data type that computes the suffix array of a string using 3-way
* radix quicksort.
*
* % java SuffixArrayX < abra.txt
* i ind lcp rnk select
* ---------------------------
* 0 11 - 0 !
* 1 10 0 1 A!
* 2 7 1 2 ABRA!
* 3 0 4 3 ABRACADABRA!
* 4 3 1 4 ACADABRA!
* 5 5 1 5 ADABRA!
* 6 8 0 6 BRA!
* 7 1 3 7 BRACADABRA!
* 8 4 0 8 CADABRA!
* 9 6 0 9 DABRA!
* 10 9 0 10 RA!
* 11 2 2 11 RACADABRA!
*
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code SuffixArrayX} class represents a suffix array of a string of
* length <em>n</em>.
* It supports the <em>selecting</em> the <em>i</em>th smallest suffix,
* getting the <em>index</em> of the <em>i</em>th smallest suffix,
* computing the length of the <em>longest common prefix</em> between the
* <em>i</em>th smallest suffix and the <em>i</em>-1st smallest suffix,
* and determining the <em>rank</em> of a query string (which is the number
* of suffixes strictly less than the query string).
* <p>
* This implementation uses 3-way radix quicksort to sort the array of suffixes.
* For a simpler (but less efficient) implementations of the same API, see
* {@link SuffixArray}.
* The <em>index</em> and <em>length</em> operations takes constant time
* in the worst case. The <em>lcp</em> operation takes time proportional to the
* length of the longest common prefix.
* The <em>select</em> operation takes time proportional
* to the length of the suffix and should be used primarily for debugging.
* <p>
* This implementation uses '\0' as a sentinel and assumes that the charater
* '\0' does not appear in the text.
* <p>
* In practice, this algorithm runs very fast. However, in the worst-case
* it can be very poor (e.g., a string consisting of N copies of the same
* character. We do not shuffle the array of suffixes before sorting because
* shuffling is relatively expensive and a pathologial input for which
* the suffixes start out in a bad order (e.g., sorted) is likely to be
* a bad input for this algorithm with or without the shuffle.
* <p>
* For additional documentation, see <a href="http://algs4.cs.princeton.edu/63suffix">Section 6.3</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*/
public class SuffixArrayX {
private static final int CUTOFF = 5; // cutoff to insertion sort (any value between 0 and 12)
private final char[] text;
private final int[] index; // index[i] = j means text.substring(j) is ith largest suffix
private final int n; // number of characters in text
/**
* Initializes a suffix array for the given {@code text} string.
* @param text the input string
*/
public SuffixArrayX(String text) {
n = text.length();
text = text + '\0';
this.text = text.toCharArray();
this.index = new int[n];
for (int i = 0; i < n; i++)
index[i] = i;
sort(0, n-1, 0);
}
// 3-way string quicksort lo..hi starting at dth character
private void sort(int lo, int hi, int d) {
// cutoff to insertion sort for small subarrays
if (hi <= lo + CUTOFF) {
insertion(lo, hi, d);
return;
}
int lt = lo, gt = hi;
char v = text[index[lo] + d];
int i = lo + 1;
while (i <= gt) {
char t = text[index[i] + d];
if (t < v) exch(lt++, i++);
else if (t > v) exch(i, gt--);
else i++;
}
// a[lo..lt-1] < v = a[lt..gt] < a[gt+1..hi].
sort(lo, lt-1, d);
if (v > 0) sort(lt, gt, d+1);
sort(gt+1, hi, d);
}
// sort from a[lo] to a[hi], starting at the dth character
private void insertion(int lo, int hi, int d) {
for (int i = lo; i <= hi; i++)
for (int j = i; j > lo && less(index[j], index[j-1], d); j--)
exch(j, j-1);
}
// is text[i+d..n) < text[j+d..n) ?
private boolean less(int i, int j, int d) {
if (i == j) return false;
i = i + d;
j = j + d;
while (i < n && j < n) {
if (text[i] < text[j]) return true;
if (text[i] > text[j]) return false;
i++;
j++;
}
return i > j;
}
// exchange index[i] and index[j]
private void exch(int i, int j) {
int swap = index[i];
index[i] = index[j];
index[j] = swap;
}
/**
* Returns the length of the input string.
* @return the length of the input string
*/
public int length() {
return n;
}
/**
* Returns the index into the original string of the <em>i</em>th smallest suffix.
* That is, {@code text.substring(sa.index(i))} is the <em>i</em> smallest suffix.
* @param i an integer between 0 and <em>n</em>-1
* @return the index into the original string of the <em>i</em>th smallest suffix
* @throws java.lang.IndexOutOfBoundsException unless {@code 0 <=i < n}
*/
public int index(int i) {
if (i < 0 || i >= n) throw new IndexOutOfBoundsException();
return index[i];
}
/**
* Returns the length of the longest common prefix of the <em>i</em>th
* smallest suffix and the <em>i</em>-1st smallest suffix.
* @param i an integer between 1 and <em>n</em>-1
* @return the length of the longest common prefix of the <em>i</em>th
* smallest suffix and the <em>i</em>-1st smallest suffix.
* @throws java.lang.IndexOutOfBoundsException unless {@code 1 <= i < n}
*/
public int lcp(int i) {
if (i < 1 || i >= n) throw new IndexOutOfBoundsException();
return lcp(index[i], index[i-1]);
}
// longest common prefix of text[i..n) and text[j..n)
private int lcp(int i, int j) {
int length = 0;
while (i < n && j < n) {
if (text[i] != text[j]) return length;
i++;
j++;
length++;
}
return length;
}
/**
* Returns the <em>i</em>th smallest suffix as a string.
* @param i the index
* @return the <em>i</em> smallest suffix as a string
* @throws java.lang.IndexOutOfBoundsException unless {@code 0 <= i < n}
*/
public String select(int i) {
if (i < 0 || i >= n) throw new IndexOutOfBoundsException();
return new String(text, index[i], n - index[i]);
}
/**
* Returns the number of suffixes strictly less than the {@code query} string.
* We note that {@code rank(select(i))} equals {@code i} for each {@code i}
* between 0 and <em>n</em>-1.
* @param query the query string
* @return the number of suffixes strictly less than {@code query}
*/
public int rank(String query) {
int lo = 0, hi = n - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
int cmp = compare(query, index[mid]);
if (cmp < 0) hi = mid - 1;
else if (cmp > 0) lo = mid + 1;
else return mid;
}
return lo;
}
// is query < text[i..n) ?
private int compare(String query, int i) {
int m = query.length();
int j = 0;
while (i < n && j < m) {
if (query.charAt(j) != text[i]) return query.charAt(j) - text[i];
i++;
j++;
}
if (i < n) return -1;
if (j < m) return +1;
return 0;
}
/**
* Unit tests the {@code SuffixArrayx} data type.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
String s = StdIn.readAll().replaceAll("\n", " ").trim();
SuffixArrayX suffix1 = new SuffixArrayX(s);
SuffixArray suffix2 = new SuffixArray(s);
boolean check = true;
for (int i = 0; check && i < s.length(); i++) {
if (suffix1.index(i) != suffix2.index(i)) {
StdOut.println("suffix1(" + i + ") = " + suffix1.index(i));
StdOut.println("suffix2(" + i + ") = " + suffix2.index(i));
String ith = "\"" + s.substring(suffix1.index(i), Math.min(suffix1.index(i) + 50, s.length())) + "\"";
String jth = "\"" + s.substring(suffix2.index(i), Math.min(suffix2.index(i) + 50, s.length())) + "\"";
StdOut.println(ith);
StdOut.println(jth);
check = false;
}
}
StdOut.println(" i ind lcp rnk select");
StdOut.println("---------------------------");
for (int i = 0; i < s.length(); i++) {
int index = suffix2.index(i);
String ith = "\"" + s.substring(index, Math.min(index + 50, s.length())) + "\"";
int rank = suffix2.rank(s.substring(index));
assert s.substring(index).equals(suffix2.select(i));
if (i == 0) {
StdOut.printf("%3d %3d %3s %3d %s\n", i, index, "-", rank, ith);
}
else {
// int lcp = suffix.lcp(suffix2.index(i), suffix2.index(i-1));
int lcp = suffix2.lcp(i);
StdOut.printf("%3d %3d %3d %3d %s\n", i, index, lcp, rank, ith);
}
}
}
}
/******************************************************************************
* Copyright 2002-2016, Robert Sedgewick and Kevin Wayne.
*
* This file is part of algs4.jar, which accompanies the textbook
*
* Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
* Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
* http://algs4.cs.princeton.edu
*
*
* algs4.jar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* algs4.jar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with algs4.jar. If not, see http://www.gnu.org/licenses.
******************************************************************************/