Advertisement
jules0707

CircularSuffixArray.java

Jun 7th, 2021
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. /*
  2. Aggregate score: 98.77%
  3. Correctness:  73/73 tests passed
  4. Memory:       10/10 tests passed
  5. Timing:       153/163 tests passed
  6. */
  7.  
  8.  
  9. import edu.princeton.cs.algs4.In;
  10. import java.util.Arrays;
  11.  
  12. public class CircularSuffixArray {
  13.  
  14.     private final int n;
  15.     private final CircularSuffix[] cs;
  16.  
  17.     // circular suffix array of s
  18.     public CircularSuffixArray(String s) {
  19.         if (null == s) throw new IllegalArgumentException();
  20.         this.n = s.length();
  21.         this.cs = new CircularSuffix[n];
  22.  
  23.         for (int i = 0; i < n; i++) { // Creation of the suffixes
  24.             cs[i] = new CircularSuffix(s, i);
  25.         }
  26.         sortCsa(); // sort the suffixes
  27.     }
  28.  
  29.     // length of s
  30.     public int length() {
  31.         return n;
  32.     }
  33.  
  34.     // returns index of ith sorted suffix
  35.     public int index(int i) {
  36.         if (i < 0 || i >= n) throw new IllegalArgumentException(); // i within bounds?
  37.         return cs[i].getFirstPos();
  38.     }
  39.  
  40.     private void sortCsa() {
  41.         Arrays.sort(cs);
  42.     }
  43.  
  44.     /////////////// NESTED CLASS //////////////////
  45.     private class CircularSuffix implements Comparable<CircularSuffix> {
  46.         private String s;
  47.         private final int firstPos;
  48.         private final int n;
  49.  
  50.         public CircularSuffix(String txt, int pos) {
  51.             this.s = txt;
  52.             this.n = txt.length();
  53.             this.firstPos = pos;
  54.         }
  55.  
  56.         public char charAt(int u) {
  57.             return this.s.charAt(u);
  58.         }
  59.  
  60.         public int getFirstPos() {
  61.             return firstPos;
  62.         }
  63.  
  64.         @Override
  65.         public int compareTo(CircularSuffix circularSuffix) {
  66.             CircularSuffix that = circularSuffix;
  67.             char c1 = charAt(this.firstPos);
  68.             char c2 = charAt(that.firstPos);
  69.             int c = 0; // a counter
  70.  
  71.             while (c1 == c2 && c < n) { // we compare the char of each suffixes at their respective first positions
  72.                 c1 = charAt((this.firstPos + c) % n); // we continue searching further down the string
  73.                 c2 = charAt((that.firstPos + c) % n);
  74.                 c++;
  75.             }
  76.             if (c == n)
  77.                 return 0; // we have reached the end of the compares without a difference so same suffixes
  78.             else return c1 < c2 ? -1 : 1;
  79.         }
  80.     }
  81.  
  82.     // unit testing (required)
  83.     public static void main(String[] args) {
  84.         In in = new In(args[0]);
  85.         String s = in.readAll();
  86.         CircularSuffixArray csa = new CircularSuffixArray(s);
  87.     }
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement