public void Compress() { Alphabet DNA = new Alphabet("ACTG"); string s = BinaryStdIn.ReadString(); int n = s.Length; BinaryStdOut.Write(n); for (int i = 0; i < n; i++) { //将字符用双位编码代码来表示 int d = DNA.ToIndex(s[i]); BinaryStdOut.Write(d, DNA.LgR()); } }
public void Compress() { //读取输入 string s = BinaryStdIn.ReadString(); char[] input = s.ToCharArray(); //统计频率 int[] freq = new int[R]; for (int i = 0; i < input.Length; i++) { freq[input[i]]++; } Node root = BuildTrie(freq); //构造编译表 string[] st = BuildCode(root); Writetrie(root); //打印总字符数 BinaryStdOut.Write(input.Length); for (int i = 0; i < input.Length; i++) { string code = st[input[i]]; for (int j = 0; j < code.Length; j++) { if (code[j] == '1') { BinaryStdOut.Write(true); } else { BinaryStdOut.Write(false); } } } }
public void Compress() { string input = BinaryStdIn.ReadString(); 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) //将s加入符号表 { st.Put(input.Substring(0, t + 1), code++); } input = input.Substring(t); } BinaryStdOut.Write(R, W); }