Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. // Circular Suffix Array
  2. // 11 October 2016
  3. // Magnus M. Halldorsson
  4. package s5;
  5.  
  6. import java.util.Arrays;
  7.  
  8. import edu.princeton.cs.algs4.In;
  9. import edu.princeton.cs.algs4.StdOut;
  10.  
  11. public class CircularSuffixArray {
  12.    
  13.     private class Node implements Comparable<Node>
  14.     {
  15.         private int index;
  16.         private String input;
  17.        
  18.         public Node(int i, String s)
  19.         {
  20.             index = i;
  21.             input = s;
  22.         }
  23.        
  24.         public int getIndex()
  25.         {
  26.             return index;
  27.         }
  28.  
  29.         @Override
  30.         public int compareTo(Node that) {
  31.             return this.input.compareTo(that.input);
  32.         }
  33.     }
  34.    
  35.     private String input;
  36.     private Node[] suffixes;
  37.    
  38.     public CircularSuffixArray(String s)
  39.     {
  40.         if(s == null)
  41.             throw new java.lang.NullPointerException("Argument is null");
  42.        
  43.         input = s;
  44.         suffixes = new Node[length()];
  45.        
  46.         for(int i = 0; i < length(); i++)
  47.         {
  48.             suffixes[i] = new Node(i, s);
  49.             s = shift(s);
  50.         }
  51.        
  52.         Arrays.sort(suffixes);
  53.     }
  54.    
  55.     public String shift(String s)
  56.     {
  57.         char[] arr = new char[length()];
  58.         arr = s.toCharArray();
  59.        
  60.         char first = arr[0];
  61.        
  62.         for(int i = 0; i < length() - 1; i++)
  63.             arr[i] = arr[i + 1];
  64.            
  65.         arr[length() - 1] = first;
  66.         String result = new String(arr);
  67.        
  68.         return result;
  69.     }
  70.  
  71.     /**
  72.      * Returns the length of the input string.
  73.      * @return the length of the input string
  74.      */
  75.     public int length() // length of s
  76.     {
  77.         return input.length();
  78.     }
  79.    
  80.     /**
  81.      * Returns the index into the original string of the <em>i</em>th smallest circular suffix.
  82.      * That is, {@code text.substring(sa.index(i))} is the <em>i</em>th smallest circular suffix.
  83.      * @param i an integer between 0 and <em>n</em>-1
  84.      * @return the index into the original string of the <em>i</em>th smallest suffix
  85.      * @throws java.lang.IndexOutOfBoundsException unless {@code 0 <= i < n}
  86.      */
  87.     public int index(int i) // returns index of ith sorted suffix
  88.     {
  89.         if(!(0 < i || i < length()))
  90.             throw new java.lang.IndexOutOfBoundsException("i is outside its prescribed range");
  91.        
  92.         return suffixes[i].getIndex();
  93.     }
  94.    
  95.     public static void main(String[] args) // unit testing
  96.     {
  97.        In in = new In(args[0]);
  98.        String s = in.readAll();  // Read whole file
  99.        String pair = s + s;
  100.        CircularSuffixArray suffix = new CircularSuffixArray(s);
  101.        
  102.        StdOut.println("  i ind select");
  103.        StdOut.println("-------------------");
  104.        
  105.        for (int i = 0; i < s.length(); i++) {
  106.            int index = suffix.index(i);
  107.            String ith = "\"" + pair.substring(index, index+Math.min(index + 50, s.length())) + "\"";
  108.            StdOut.printf("%3d %3d %s\n", i, index, ith);
  109.        }   
  110.     }  
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement