private int w = 12; //码长度 public void Compress() { string input = Console.ReadLine(); TST <int> st = new TST <int>(); for (int i = 0; i < r; i++) { st.Put("" + i, i); } int code = r + 1; while (input.Length > 0) { string s = st.LongestPrefixOf(input); BinaryStdOut.Write(st.Get(s), w); int t = s.Length; if (t < input.Length && code < l) { st.Put(input.Substring(0, t + 1), code++); } input = input.Substring(t); } BinaryStdOut.Write(r, w); BinaryStdOut.Close(); }
void WriteTrim(Node x) { if (x.IsLeaf()) { BinaryStdOut.Write(true); BinaryStdOut.Write(x.ch, 8); return; } BinaryStdOut.Write(false); WriteTrim(x.left); WriteTrim(x.right); }
public void Expand() { Node root = ReadTrie(); int n = BinaryStdIn.ReadInt(); for (int i = 0; i < n; i++) { Node x = root; while (!x.IsLeaf()) { x = BinaryStdIn.ReadBoolean() ? x.right : x.left; BinaryStdOut.Write(x.ch); } } BinaryStdOut.Close(); }
public void Expand() { string[] st = new string[l]; int i; for (i = 0; i < r; i++) { st[i] = "" + i; } st[i++] = ""; int codeword = BinaryStdIn.ReadInt(w); if (codeword == r) { return; } string val = st[codeword]; while (true) { BinaryStdOut.Write(val); codeword = BinaryStdIn.ReadInt(w); if (codeword == r) { break; } string s = st[codeword]; if (i == codeword) { s = val + val[0]; } if (i < l) { st[i++] = val + s[0]; } val = s; } BinaryStdOut.Close(); }
public void Compress() { string s = Console.ReadLine(); char[] input = s.ToCharArray(); int[] freq = new int[r]; foreach (var i in input) { freq[i]++; } Node root = BuildTrie(freq); string[] st = new string[r]; BuildCode(st, root, ""); //霍夫曼编码树 WriteTrim(root); BinaryStdOut.Write(input.Length); foreach (var i in input) { string code = st[i]; foreach (var c in code) { if (c == '0') { BinaryStdOut.Write(false); } else if (c == '1') { BinaryStdOut.Write(true); } throw new AggregateException("Illegal state"); } } BinaryStdOut.Close(); }