-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularSuffixArray.java
More file actions
66 lines (56 loc) · 1.28 KB
/
CircularSuffixArray.java
File metadata and controls
66 lines (56 loc) · 1.28 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
import java.util.Arrays;
import java.util.Comparator;
public class CircularSuffixArray {
private char array[];
private Integer id[];
private int length;
// circular suffix array of s
public CircularSuffixArray(String s) {
if (s==null) {
throw new java.lang.IllegalArgumentException();
}
length = s.length();
array = new char [length];
id = new Integer [length];
for (int i=0; i<s.length(); i++) {
array[i] = s.charAt(i);
id[i] = i;
}
Arrays.sort(id, new SuffixComp());
}
// length of s
public int length() {
return length;
}
// returns index of ith sorted suffix
public int index(int i) {
if (i<0 || i >= length()) {
throw new java.lang.IllegalArgumentException();
}
return id[i];
}
private class SuffixComp implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
for (int i=0; i<length(); i++) {
int c1=array[(o1+i)%length];
int c2=array[(o2+i)%length];
if (c1 > c2) {
return 1;
} else if (c1 < c2) {
return -1;
} else {
continue;
}
}
return 0;
}
}
// unit testing (required)
public static void main(String[] args) {
CircularSuffixArray a = new CircularSuffixArray("ABRACADABRA!");
for (int i=0; i<a.length(); i++) {
System.out.println(a.index(i));
}
}
}