Advertisement
jules0707

BurrowsWheeler.java

Jun 7th, 2021
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. import edu.princeton.cs.algs4.BinaryStdIn;
  2. import edu.princeton.cs.algs4.BinaryStdOut;
  3. import java.util.Arrays;
  4.  
  5. public class BurrowsWheeler {
  6.  
  7.     // apply Burrows-Wheeler transform,
  8.     // reading from standard input and writing to standard output
  9.     public static void transform() {
  10.         String txt = BinaryStdIn.readString();
  11.         CircularSuffixArray csa = new CircularSuffixArray(txt);
  12.         for (int i = 0; i < txt.length(); i++) { // search i
  13.             if (csa.index(i) == 0)
  14.                 BinaryStdOut.write(i);
  15.         }
  16.         for (int j = 0; j < txt.length(); j++) {
  17.             int idx = csa.index(j);
  18.             char ch = txt.charAt((idx - 1 + txt.length()) % txt.length());
  19.             BinaryStdOut.write(ch);
  20.         }
  21.         BinaryStdOut.close();
  22.     }
  23.  
  24.     // apply Burrows-Wheeler inverse transform,
  25.     // reading from standard input and writing to standard output
  26.     public static void inverseTransform() {
  27.         int first = BinaryStdIn.readInt();
  28.         String txt = BinaryStdIn.readString();
  29.         char[] a = txt.toCharArray();
  30.         int n = a.length;
  31.         int r = 256;
  32.         int[] count = new int[r + 1];
  33.         char[] aux = new char[n]; // the sorted array
  34.         int[] next = new int[n];
  35.  
  36.         // ==== Key Index Counting Algo ===//
  37.         // first pass: count the frequencies of each letter in txt using the key as an index.
  38.         for (int i = 0; i < n; i++) { // ie. the number of As, Bs, Ds, Es, Fs etc.
  39.             count[a[i] + 1]++; // increment the count of each index[] keys
  40.         }
  41.         // second pass: count the cumulates
  42.         for (int i = 0; i < r; i++) {
  43.             count[i + 1] += count[i];
  44.         }
  45.         // third pass: distribute the items in sorted order. The value of next[i] is
  46.         // exactly the same as the location in the new array where the value is being copied to.
  47.         for (int i = 0; i < n; i++) {
  48.             int p = count[a[i]]++;
  49.             aux[p] = a[i];
  50.             next[p] = i;
  51.         }
  52.         BinaryStdOut.write(reconstructString(first, aux, next));
  53.         BinaryStdOut.close();
  54.     }
  55.  
  56.     private static String reconstructString(int first, char[] aux, int[] next) {
  57.         int n = next.length;
  58.         char[] res = new char[n];
  59.         int[] copy = Arrays.copyOf(next, n);
  60.         for (int i = 0; i < n; i++) {
  61.             res[i] = aux[first];
  62.             first = copy[first];
  63.         }
  64.         return String.valueOf(res);
  65.     }
  66.  
  67.     // if args[0] is "-", apply Burrows-Wheeler transform
  68.     // if args[0] is "+", apply Burrows-Wheeler inverse transform
  69.     public static void main(String[] args) {
  70.         if (args[0].equals("-")) transform();
  71.         else if (args[0].equals("+")) inverseTransform();
  72.         else throw new IllegalArgumentException("Illegal command line argument");
  73.     }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement