-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircularSuffixArray.java
75 lines (62 loc) · 1.82 KB
/
CircularSuffixArray.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
public class CircularSuffixArray {
private int N;
private String a;
private int[] index;
private final int R = 256;
public CircularSuffixArray(String s) // circular suffix array of s
{
this.N = s.length();
this.a = s;
this.index = new int[N];
}
public int length() // length of s
{
return N;
}
public int index(int j) // returns index of ith sorted suffix
// public void index() // returns index of ith sorted suffix
{
for (int k=0;k<N;k++) {
this.index[k] = k;
}
int[] index_aux = new int[N];
for (int d = N-1; d >= 0; d--) {
int[] count = new int[R+1];
for (int i = 0; i < N; i++)
count[a.charAt((index[i]+d)%N) +1]++;
for (int r = 0; r < R; r++)
count[r+1] += count[r];
for (int i = 0; i < N; i++) {
index_aux[count[a.charAt((index[i]+d)%N)]] = index[i];
count[a.charAt((index[i]+d)%N)]++;
}
for (int i = 0; i < N; i++) {
index[i] = index_aux[i];
}
//System.out.println(" ");
//for (int i=0; i < N; i++) {
// System.out.print(index[i]+" ");
//}
}
return index[j];
}
public static void main(String[] args)// unit testing of the methods (optional)
{
char[] a = new char[12];
a[0]='d';
a[1]='a';
a[2]='c';
a[3]='f';
a[4]='f';
a[5]='b';
a[6]='d';
a[7]='b';
a[8]='f';
a[9]='b';
a[10]='e';
a[11]='a';
String b = "ABRACADABRA!";
CircularSuffixArray csa = new CircularSuffixArray(b);
System.out.println(csa.index(11));
}
}