Ejemplo n.º 1
0
        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();
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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();
        }
Ejemplo n.º 4
0
        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();
        }
Ejemplo n.º 5
0
        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();
        }