-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuffix_Arrays.java
222 lines (195 loc) · 7.91 KB
/
Suffix_Arrays.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
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
SuffixArray.java
Below is the syntax highlighted version of SuffixArray.java from §6.3 Suffix Arrays.
/******************************************************************************
* Compilation: javac SuffixArray.java
* Execution: java SuffixArray < input.txt
* Dependencies: StdIn.java StdOut.java
*
* A data type that computes the suffix array of a string.
*
* % java SuffixArray < 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!"
*
* See SuffixArrayX.java for an optimized version that uses 3-way
* radix quicksort and does not use the nested class Suffix.
*
******************************************************************************/
import java.util.Arrays;
/**
* The <tt>SuffixArray</tt> 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 a nested class <tt>Suffix</tt> to represent
* a suffix of a string (using constant time and space) and
* <tt>Arrays.sort()</tt> to sort the array of suffixes.
* For alternate implementations of the same API, see
* {@link SuffixArrayX}, which is faster in practice (uses 3-way radix quicksort)
* and uses less memory (does not create <tt>Suffix</tt> objects).
* 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>
* 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 SuffixArray {
private Suffix[] suffixes;
/**
* Initializes a suffix array for the given <tt>text</tt> string.
* @param text the input string
*/
public SuffixArray(String text) {
int N = text.length();
this.suffixes = new Suffix[N];
for (int i = 0; i < N; i++)
suffixes[i] = new Suffix(text, i);
Arrays.sort(suffixes);
}
private static class Suffix implements Comparable<Suffix> {
private final String text;
private final int index;
private Suffix(String text, int index) {
this.text = text;
this.index = index;
}
private int length() {
return text.length() - index;
}
private char charAt(int i) {
return text.charAt(index + i);
}
public int compareTo(Suffix that) {
if (this == that) return 0; // optimization
int N = Math.min(this.length(), that.length());
for (int i = 0; i < N; i++) {
if (this.charAt(i) < that.charAt(i)) return -1;
if (this.charAt(i) > that.charAt(i)) return +1;
}
return this.length() - that.length();
}
public String toString() {
return text.substring(index);
}
}
/**
* Returns the length of the input string.
* @return the length of the input string
*/
public int length() {
return suffixes.length;
}
/**
* Returns the index into the original string of the <em>i</em>th smallest suffix.
* That is, <tt>text.substring(sa.index(i))</tt> is the <em>i</em>th 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 0 ≤ <em>i</em> < <Em>N</em>
*/
public int index(int i) {
if (i < 0 || i >= suffixes.length) throw new IndexOutOfBoundsException();
return suffixes[i].index;
}
/**
* 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 1 ≤ <em>i</em> < <em>N</em>
*/
public int lcp(int i) {
if (i < 1 || i >= suffixes.length) throw new IndexOutOfBoundsException();
return lcp(suffixes[i], suffixes[i-1]);
}
// longest common prefix of s and t
private static int lcp(Suffix s, Suffix t) {
int N = Math.min(s.length(), t.length());
for (int i = 0; i < N; i++) {
if (s.charAt(i) != t.charAt(i)) return i;
}
return N;
}
/**
* 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 0 ≤ <em>i</em> < <Em>N</em>
*/
public String select(int i) {
if (i < 0 || i >= suffixes.length) throw new IndexOutOfBoundsException();
return suffixes[i].toString();
}
/**
* Returns the number of suffixes strictly less than the <tt>query</tt> string.
* We note that <tt>rank(select(i))</tt> equals <tt>i</tt> for each <tt>i</tt>
* between 0 and <em>N</em>-1.
* @param query the query string
* @return the number of suffixes strictly less than <tt>query</tt>
*/
public int rank(String query) {
int lo = 0, hi = suffixes.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
int cmp = compare(query, suffixes[mid]);
if (cmp < 0) hi = mid - 1;
else if (cmp > 0) lo = mid + 1;
else return mid;
}
return lo;
}
// compare query string to suffix
private static int compare(String query, Suffix suffix) {
int N = Math.min(query.length(), suffix.length());
for (int i = 0; i < N; i++) {
if (query.charAt(i) < suffix.charAt(i)) return -1;
if (query.charAt(i) > suffix.charAt(i)) return +1;
}
return query.length() - suffix.length();
}
/**
* Unit tests the <tt>SuffixArray</tt> data type.
*/
public static void main(String[] args) {
String s = StdIn.readAll().replaceAll("\\s+", " ").trim();
SuffixArray suffix = new SuffixArray(s);
// StdOut.println("rank(" + args[0] + ") = " + suffix.rank(args[0]));
StdOut.println(" i ind lcp rnk select");
StdOut.println("---------------------------");
for (int i = 0; i < s.length(); i++) {
int index = suffix.index(i);
String ith = "\"" + s.substring(index, Math.min(index + 50, s.length())) + "\"";
assert s.substring(index).equals(suffix.select(i));
int rank = suffix.rank(s.substring(index));
if (i == 0) {
StdOut.printf("%3d %3d %3s %3d %s\n", i, index, "-", rank, ith);
}
else {
int lcp = suffix.lcp(i);
StdOut.printf("%3d %3d %3d %3d %s\n", i, index, lcp, rank, ith);
}
}
}
}
Copyright © 2002–2010, Robert Sedgewick and Kevin Wayne.
Last updated: Wed Sept 16 05:30:12 IST 2015.