Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class BurrowsWheeler {
  4. // apply Burrows-Wheeler encoding, reading from standard input and writing to standard output
  5. public static void encode()
  6. {
  7. String s = BinaryStdIn.readString();
  8. int length = s.length();
  9. int index = 0;
  10. String[] permutations = permutate(s, length);
  11. Arrays.sort(permutations);
  12. char[] encoded = new char[length];
  13. for(int i = 0; i < length; i++) {
  14. encoded[i] = permutations[i].charAt(length-1);
  15. if(permutations[i].equals(s)) index = i;
  16. }
  17. BinaryStdOut.write(index);
  18. BinaryStdOut.write(String.valueOf(encoded));
  19. StdOut.println(String.valueOf(encoded));
  20. }
  21. // apply Burrows-Wheeler decoding, reading from standard input and writing to standard output
  22. public static void decode()
  23. {
  24. int sortedRow = BinaryStdIn.readInt();
  25. String encoded = BinaryStdIn.readString();
  26. char[] charList = encoded.toCharArray();
  27. int length = charList.length;
  28. //Sorted suffix array and next array
  29. char[][] suffix = new char[length][length];
  30. int[] next = new int[length];
  31. for(int i = 0; i < length; i++) {
  32. suffix[i][length-1] = charList[i];
  33. }
  34. Arrays.sort(charList);
  35. for(int i = 0; i < length; i++) {
  36. suffix[i][0] = charList[i];
  37. }
  38.  
  39. int repeat = 0;
  40. int rrpeat;
  41. for(int i = 0; i < length; i++) {
  42. if(i != 0) {
  43. if(suffix[i-1][0] == (suffix[i][0])) repeat += 1;
  44. else repeat = 0;
  45. }
  46. rrpeat = repeat;
  47. for(int j = 0; j < length; j++) {
  48. //int rrpeat = repeat;
  49. if(j != i && suffix[i][0] == suffix[j][length-1] && repeat == 0) {
  50. next[i] = j;
  51. break;
  52. }
  53. else if(j != i && suffix[i][0] == suffix[j][length-1] && repeat > 0) {
  54. repeat--;
  55. continue;
  56. }
  57. }
  58. repeat = rrpeat;
  59. }
  60. char[] original = new char[length];
  61. for(int i = 0; i < length; i++) {
  62. original[i] = suffix[sortedRow][0];
  63. sortedRow = next[sortedRow];
  64. }
  65. BinaryStdOut.write(String.valueOf(original));
  66.  
  67. }
  68. // if args[0] is '-', apply Burrows-Wheeler encoding
  69. // if args[0] is '+', apply Burrows-Wheeler decoding
  70. public static void main(String[] args)
  71. {
  72. if(args[0].equals("-"))encode();
  73. else if(args[0].equals("+")) decode();
  74. }
  75.  
  76. public static String[] permutate(String s, int length) {
  77. String[] permutations = new String[length];
  78. char[] stringedit = s.toCharArray();
  79. permutations[0] = String.valueOf(stringedit);
  80. for(int i = 1; i < length; i++) {
  81. char lastchar = stringedit[0];
  82. char temp = stringedit[1];
  83. //move characters left by 1
  84. for(int j = length-1; j >= 0; j--) {
  85. temp = stringedit[j];
  86. stringedit[j] = lastchar;
  87. lastchar = temp;
  88. }
  89. //stringedit[0] = ;
  90. permutations[i] = String.valueOf(stringedit);
  91. }
  92.  
  93. return permutations;
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement