fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. int bits,len,i,n;
  14. final int MAX_BITS = 9; //4;
  15. final int max_code = 287; //8;
  16. long code;
  17. long[] bl_count = new long[MAX_BITS+1],
  18. next_code = new long[MAX_BITS+1];
  19. Tree[] tree = new Tree[max_code+1];
  20.  
  21. /*
  22. tree[0] = new Tree(0, 0);
  23. tree[1] = new Tree(1, 3);
  24. tree[2] = new Tree(2, 3);
  25. tree[3] = new Tree(3, 3);
  26. tree[4] = new Tree(4, 3);
  27. tree[5] = new Tree(5, 3);
  28. tree[6] = new Tree(6, 2);
  29. tree[7] = new Tree(7, 4);
  30. tree[8] = new Tree(8, 4);
  31. */
  32.  
  33. for (i = 0; i < 144; i++)
  34. tree[i] = new Tree(i, 8);
  35. for (; i < 256; i++)
  36. tree[i] = new Tree(i, 9);
  37. for (; i < 280; i++)
  38. tree[i] = new Tree(i, 7);
  39. for (; i < 288; i++)
  40. tree[i] = new Tree(i, 8);
  41.  
  42. //Step1
  43. for (i = 0; i < tree.length; i++) {
  44. bl_count[tree[i].Len]++;
  45. }
  46. System.out.println("Step1");
  47. for (i = 2; i < bl_count.length; i++)
  48. System.out.printf("%d: %d\n", i, bl_count[i]);
  49.  
  50. //Step2
  51. code = 0;
  52. bl_count[0] = 0;
  53. for (bits = 1; bits <= MAX_BITS; bits++) {
  54. code = (code + bl_count[bits-1]) << 1;
  55. next_code[bits] = code;
  56. }
  57. System.out.println("Step2");
  58. for (i = 1; i < next_code.length; i++)
  59. System.out.printf("%d: %d\n", i, next_code[i]);
  60.  
  61. //Step3
  62. for (n = 0; n <= max_code; n++) {
  63. len = tree[n].Len;
  64. if (len != 0) {
  65. tree[n].Code = next_code[len];
  66. next_code[len]++;
  67. }
  68. }
  69. System.out.println("Step3");
  70. for (i = 0; i <= max_code; i++) {
  71. String s = "0000000000" + Long.toString(tree[i].Code, 2);
  72. s = s.substring(s.length() - tree[i].Len);
  73. System.out.printf("%03d: ", i);
  74. System.out.println(s);
  75. }
  76.  
  77. }
  78.  
  79. public static class Tree {
  80. public int Len = 0;
  81. public long Code = 0;
  82. public Tree(long code, int len) {
  83. Len = len; Code = code;
  84. }
  85. }
  86. }
Success #stdin #stdout 0.14s 380480KB
stdin
Standard input is empty
stdout
Step1
1: 0
2: 0
3: 0
4: 0
5: 0
6: 0
7: 24
8: 152
9: 112
Step2
1: 0
2: 0
3: 0
4: 0
5: 0
6: 0
7: 0
8: 48
9: 400
Step3
000: 00110000
001: 00110001
002: 00110010
003: 00110011
004: 00110100
005: 00110101
006: 00110110
007: 00110111
008: 00111000
009: 00111001
010: 00111010
011: 00111011
012: 00111100
013: 00111101
014: 00111110
015: 00111111
016: 01000000
017: 01000001
018: 01000010
019: 01000011
020: 01000100
021: 01000101
022: 01000110
023: 01000111
024: 01001000
025: 01001001
026: 01001010
027: 01001011
028: 01001100
029: 01001101
030: 01001110
031: 01001111
032: 01010000
033: 01010001
034: 01010010
035: 01010011
036: 01010100
037: 01010101
038: 01010110
039: 01010111
040: 01011000
041: 01011001
042: 01011010
043: 01011011
044: 01011100
045: 01011101
046: 01011110
047: 01011111
048: 01100000
049: 01100001
050: 01100010
051: 01100011
052: 01100100
053: 01100101
054: 01100110
055: 01100111
056: 01101000
057: 01101001
058: 01101010
059: 01101011
060: 01101100
061: 01101101
062: 01101110
063: 01101111
064: 01110000
065: 01110001
066: 01110010
067: 01110011
068: 01110100
069: 01110101
070: 01110110
071: 01110111
072: 01111000
073: 01111001
074: 01111010
075: 01111011
076: 01111100
077: 01111101
078: 01111110
079: 01111111
080: 10000000
081: 10000001
082: 10000010
083: 10000011
084: 10000100
085: 10000101
086: 10000110
087: 10000111
088: 10001000
089: 10001001
090: 10001010
091: 10001011
092: 10001100
093: 10001101
094: 10001110
095: 10001111
096: 10010000
097: 10010001
098: 10010010
099: 10010011
100: 10010100
101: 10010101
102: 10010110
103: 10010111
104: 10011000
105: 10011001
106: 10011010
107: 10011011
108: 10011100
109: 10011101
110: 10011110
111: 10011111
112: 10100000
113: 10100001
114: 10100010
115: 10100011
116: 10100100
117: 10100101
118: 10100110
119: 10100111
120: 10101000
121: 10101001
122: 10101010
123: 10101011
124: 10101100
125: 10101101
126: 10101110
127: 10101111
128: 10110000
129: 10110001
130: 10110010
131: 10110011
132: 10110100
133: 10110101
134: 10110110
135: 10110111
136: 10111000
137: 10111001
138: 10111010
139: 10111011
140: 10111100
141: 10111101
142: 10111110
143: 10111111
144: 110010000
145: 110010001
146: 110010010
147: 110010011
148: 110010100
149: 110010101
150: 110010110
151: 110010111
152: 110011000
153: 110011001
154: 110011010
155: 110011011
156: 110011100
157: 110011101
158: 110011110
159: 110011111
160: 110100000
161: 110100001
162: 110100010
163: 110100011
164: 110100100
165: 110100101
166: 110100110
167: 110100111
168: 110101000
169: 110101001
170: 110101010
171: 110101011
172: 110101100
173: 110101101
174: 110101110
175: 110101111
176: 110110000
177: 110110001
178: 110110010
179: 110110011
180: 110110100
181: 110110101
182: 110110110
183: 110110111
184: 110111000
185: 110111001
186: 110111010
187: 110111011
188: 110111100
189: 110111101
190: 110111110
191: 110111111
192: 111000000
193: 111000001
194: 111000010
195: 111000011
196: 111000100
197: 111000101
198: 111000110
199: 111000111
200: 111001000
201: 111001001
202: 111001010
203: 111001011
204: 111001100
205: 111001101
206: 111001110
207: 111001111
208: 111010000
209: 111010001
210: 111010010
211: 111010011
212: 111010100
213: 111010101
214: 111010110
215: 111010111
216: 111011000
217: 111011001
218: 111011010
219: 111011011
220: 111011100
221: 111011101
222: 111011110
223: 111011111
224: 111100000
225: 111100001
226: 111100010
227: 111100011
228: 111100100
229: 111100101
230: 111100110
231: 111100111
232: 111101000
233: 111101001
234: 111101010
235: 111101011
236: 111101100
237: 111101101
238: 111101110
239: 111101111
240: 111110000
241: 111110001
242: 111110010
243: 111110011
244: 111110100
245: 111110101
246: 111110110
247: 111110111
248: 111111000
249: 111111001
250: 111111010
251: 111111011
252: 111111100
253: 111111101
254: 111111110
255: 111111111
256: 0000000
257: 0000001
258: 0000010
259: 0000011
260: 0000100
261: 0000101
262: 0000110
263: 0000111
264: 0001000
265: 0001001
266: 0001010
267: 0001011
268: 0001100
269: 0001101
270: 0001110
271: 0001111
272: 0010000
273: 0010001
274: 0010010
275: 0010011
276: 0010100
277: 0010101
278: 0010110
279: 0010111
280: 11000000
281: 11000001
282: 11000010
283: 11000011
284: 11000100
285: 11000101
286: 11000110
287: 11000111