Beispiel #1
0
        public void Expand()
        {
            string[] st = new string[L];
            int      i; // 下一个待补全的编码值

            for (i = 0; i < R; i++)
            {
                st[i] = "" + i;
            }
            st[i++] = " ";//EOF
            int    codeword = BinaryStdIn.ReadInt(W);
            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;
            }
        }
        public void Compress()
        {
            char cnt = (char)0;
            bool b, old = false;

            while (!BinaryStdIn.IsEmpty())
            {
                b = BinaryStdIn.ReadBoolean();
                if (b != old)
                {
                    BinaryStdOut.Write(cnt);
                    cnt = (char)0;
                    old = !old;
                }
                else
                {
                    if (cnt == 255)
                    {
                        BinaryStdOut.Write(cnt);
                        cnt = (char)0;
                        BinaryStdOut.Write(cnt);
                    }
                }
                cnt++;
            }
            BinaryStdOut.Write(cnt);
        }
 private static void Writetrie(Node x)
 {
     if (x.isLeaf())
     {
         BinaryStdOut.Write(true);
         BinaryStdOut.Write(x.Ch);
         return;
     }
     BinaryStdOut.Write(false);
     Writetrie(x.Left);
     Writetrie(x.Right);
 }
        public void Expand()
        {
            Alphabet DNA = new Alphabet("ACTG");
            int      w   = DNA.LgR();
            int      n   = BinaryStdIn.ReadInt();

            for (int i = 0; i < n; i++)
            {
                var c = BinaryStdIn.ReadChar(w);
                BinaryStdOut.Write(DNA.ToChar(c));
            }
        }
        public void Expand()
        {
            bool b = false;

            while (!BinaryStdIn.IsEmpty())
            {
                char cnt = BinaryStdIn.ReadChar();
                for (int i = 0; i < cnt; i++)
                {
                    BinaryStdOut.Write(b);
                }
                b = !b;
            }
        }
        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 Expand()
        {
            Node root = ReadTrie();
            int  n    = BinaryStdIn.ReadInt();

            for (int i = 0; i < n; i++)
            {
                var x = root;
                while (!x.isLeaf())
                {
                    if (BinaryStdIn.ReadBoolean())
                    {
                        x = x.Right;
                    }
                    else
                    {
                        x = x.Left;
                    }
                }
                BinaryStdOut.Write(x.Ch);
            }
        }
        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);
                    }
                }
            }
        }
Beispiel #9
0
        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);
        }