Esempio n. 1
0
    public static void compress()
    {
        string text = BinaryStdIn.readString();
        TST    tST  = new TST();
        int    i;

        for (i = 0; i < 256; i++)
        {
            tST.put(new StringBuilder().append("").append((char)i).toString(), Integer.valueOf(i));
        }
        i = 257;
        while (java.lang.String.instancehelper_length(text) > 0)
        {
            string text2 = tST.longestPrefixOf(text);
            BinaryStdOut.write(((Integer)tST.get(text2)).intValue(), 12);
            int num = java.lang.String.instancehelper_length(text2);
            if (num < java.lang.String.instancehelper_length(text) && i < 4096)
            {
                TST    arg_A5_0 = tST;
                string arg_A5_1 = java.lang.String.instancehelper_substring(text, 0, num + 1);
                int    arg_A0_0 = i;
                i++;
                arg_A5_0.put(arg_A5_1, Integer.valueOf(arg_A0_0));
            }
            text = java.lang.String.instancehelper_substring(text, num);
        }
        BinaryStdOut.write(256, 12);
        BinaryStdOut.close();
    }
        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);
        }
Esempio n. 3
0
        public static void compress()
        {
            int num  = 0;
            int num2 = 0;

            while (!BinaryStdIn.IsEmpty)
            {
                int num3 = BinaryStdIn.readBoolean() ? 1 : 0;
                if (num3 != num2)
                {
                    BinaryStdOut.write((char)num, 8);
                    num  = 1;
                    num2 = ((num2 != 0) ? 0 : 1);
                }
                else
                {
                    if (num == 255)
                    {
                        BinaryStdOut.write((char)num, 8);
                        num = 0;
                        BinaryStdOut.write((char)num, 8);
                    }
                    num = (int)((ushort)(num + 1));
                }
            }
            BinaryStdOut.write((char)num, 8);
            BinaryStdOut.close();
        }
Esempio n. 4
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;
            }
        }
 private static Node ReadTrie()
 {
     if (BinaryStdIn.ReadBoolean())
     {
         return(new Node(BinaryStdIn.ReadChar(), 0, null, null));
     }
     return(new Node('\0', 0, ReadTrie(), ReadTrie()));
 }
Esempio n. 6
0
 /**/
 public static void main(string[] strarr)
 {
     while (!BinaryStdIn.IsEmpty)
     {
         int ch = (int)BinaryStdIn.readChar();
         BinaryStdOut.write((char)ch);
     }
     BinaryStdOut.flush();
 }
Esempio n. 7
0
 private static Node readTrie() {
     boolean isLeaf = BinaryStdIn.readBoolean();
     if (isLeaf) {
         return new Node(BinaryStdIn.readChar(), -1, null, null);
     }
     else {
         return new Node('\0', -1, readTrie(), readTrie());
     }
 }
Esempio n. 8
0
 /**
  * Reads a sequence of bits from standard input (that are encoded
  * using run-length encoding with 8-bit run lengths); decodes them;
  * and writes the results to standard output.
  */
 public static void expand() { 
     boolean b = false; 
     while (!BinaryStdIn.isEmpty()) {
         int run = BinaryStdIn.readInt(LG_R);
         for (int i = 0; i < run; i++)
             BinaryStdOut.write(b);
         b = !b;
     }
     BinaryStdOut.close();
 }
Esempio n. 9
0
 /**
  * Reads a binary sequence from standard input; converts each two bits
  * to an 8-bit extended ASCII character over the alphabet { A, C, T, G };
  * and writes the results to standard output.
  */
 public static void expand() {
     Alphabet DNA = Alphabet.DNA;
     int n = BinaryStdIn.readInt();
     // Read two bits; write char. 
     for (int i = 0; i < n; i++) {
         char c = BinaryStdIn.readChar(2);
         BinaryStdOut.write(DNA.toChar(c), 8);
     }
     BinaryStdOut.close();
 }
        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));
            }
        }
Esempio n. 11
0
    /**
     * Reads a sequence of 8-bit extended ASCII characters over the alphabet
     * { A, C, T, G } from standard input; compresses them using two bits per
     * character; and writes the results to standard output.
     */
    public static void compress() { 
        Alphabet DNA = Alphabet.DNA;
        String s = BinaryStdIn.readString();
        int n = s.length();
        BinaryStdOut.write(n);

        // Write two-bit code for char. 
        for (int i = 0; i < n; i++) {
            int d = DNA.toIndex(s.charAt(i));
            BinaryStdOut.write(d, 2);
        }
        BinaryStdOut.close();
    } 
        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()
        {
            bool b = false;

            while (!BinaryStdIn.IsEmpty())
            {
                char cnt = BinaryStdIn.ReadChar();
                for (int i = 0; i < cnt; i++)
                {
                    BinaryStdOut.Write(b);
                }
                b = !b;
            }
        }
Esempio n. 14
0
        public static void expand()
        {
            int num = 0;

            while (!BinaryStdIn.IsEmpty)
            {
                int num2 = BinaryStdIn.readInt(8);
                for (int i = 0; i < num2; i++)
                {
                    BinaryStdOut.write(num != 0);
                }
                num = ((num != 0) ? 0 : 1);
            }
            BinaryStdOut.close();
        }
Esempio n. 15
0
    /**
     * Reads a sequence of 8-bit bytes from standard input; compresses
     * them using LZW compression with 12-bit codewords; and writes the results
     * to standard output.
     */
    public static void compress() { 
        String input = BinaryStdIn.readString();
        TST<Integer> st = new TST<Integer>();
        for (int i = 0; i < R; i++)
            st.put("" + (char) i, i);
        int code = R+1;  // R is codeword for EOF

        while (input.length() > 0) {
            String s = st.longestPrefixOf(input);  // Find max prefix match s.
            BinaryStdOut.write(st.get(s), W);      // Print s's encoding.
            int t = s.length();
            if (t < input.length() && code < L)    // Add s to symbol table.
                st.put(input.substring(0, t + 1), code++);
            input = input.substring(t);            // Scan past s in input.
        }
        BinaryStdOut.write(R, W);
        BinaryStdOut.close();
    } 
Esempio n. 16
0
 /**
  * Reads in a sequence of bytes from standard input and draws
  * them to standard drawing output as a width-by-height picture,
  * using black for 1 and white for 0 (and red for any leftover
  * pixels).
  *
  * @param args the command-line arguments
  */
 public static void main(String[] args) {
     int width = Integer.parseInt(args[0]);
     int height = Integer.parseInt(args[1]);
     Picture picture = new Picture(width, height);
     for (int row = 0; row < height; row++) {
         for (int col = 0; col < width; col++) {
             if (!BinaryStdIn.isEmpty()) {
                 boolean bit = BinaryStdIn.readBoolean();
                 if (bit) picture.set(col, row, Color.BLACK);
                 else     picture.set(col, row, Color.WHITE);
             }
             else {
                 picture.set(col, row, Color.RED);
             }
         }
     }
     picture.show();
 }
Esempio n. 17
0
    /**
     * Reads in a sequence of bytes from standard input and writes
     * them to standard output in binary, k bits per line,
     * where k is given as a command-line integer (defaults
     * to 16 if no integer is specified); also writes the number
     * of bits.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        int bitsPerLine = 16;
        if (args.length == 1) {
            bitsPerLine = Integer.parseInt(args[0]);
        }

        int count;
        for (count = 0; !BinaryStdIn.isEmpty(); count++) {
            if (bitsPerLine == 0) {
                BinaryStdIn.readBoolean();
                continue;
            }
            else if (count != 0 && count % bitsPerLine == 0) StdOut.println();
            if (BinaryStdIn.readBoolean()) StdOut.print(1);
            else                           StdOut.print(0);
        }
        if (bitsPerLine != 0) StdOut.println();
        StdOut.println(count + " bits");
    }
Esempio n. 18
0
    /**
     * Reads a sequence of bits that represents a Huffman-compressed message from
     * standard input; expands them; and writes the results to standard output.
     */
    public static void expand() {

        // read in Huffman trie from input stream
        Node root = readTrie(); 

        // number of bytes to write
        int length = BinaryStdIn.readInt();

        // decode using the Huffman trie
        for (int i = 0; i < length; i++) {
            Node x = root;
            while (!x.isLeaf()) {
                boolean bit = BinaryStdIn.readBoolean();
                if (bit) x = x.right;
                else     x = x.left;
            }
            BinaryStdOut.write(x.ch, 8);
        }
        BinaryStdOut.close();
    }
Esempio n. 19
0
    public static void expand()
    {
        string[] array = new string[4096];
        int      i;

        for (i = 0; i < 256; i++)
        {
            array[i] = new StringBuilder().append("").append((char)i).toString();
        }
        string[] arg_44_0 = array;
        int      arg_44_1 = i;

        i++;
        arg_44_0[arg_44_1] = "";
        int    num  = BinaryStdIn.readInt(12);
        string text = array[num];

        while (true)
        {
            BinaryStdOut.write(text);
            num = BinaryStdIn.readInt(12);
            if (num == 256)
            {
                break;
            }
            string text2 = array[num];
            if (i == num)
            {
                text2 = new StringBuilder().append(text).append(java.lang.String.instancehelper_charAt(text, 0)).toString();
            }
            if (i < 4096)
            {
                string[] arg_BE_0 = array;
                int      arg_BE_1 = i;
                i++;
                arg_BE_0[arg_BE_1] = new StringBuilder().append(text).append(java.lang.String.instancehelper_charAt(text2, 0)).toString();
            }
            text = text2;
        }
        BinaryStdOut.close();
    }
Esempio n. 20
0
        static void Main(string[] args)
        {
            //LSDSort();
            //MSDSort();
            //Q3String();
            //StringSearch();
            //Regax();

            for (; !BinaryStdIn.IsEmpty();)
            {
                if (BinaryStdIn.ReadBoolean())
                {
                    StdOut.Print("1");
                }
                else
                {
                    StdOut.Print("0");
                }
            }
            StdOut.Println();
        }
Esempio n. 21
0
    /**
     * Reads in a sequence of bytes from standard input and writes
     * them to standard output using hexademical notation, k hex digits
     * per line, where k is given as a command-line integer (defaults
     * to 16 if no integer is specified); also writes the number
     * of bits.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        int bytesPerLine = 16;
        if (args.length == 1) {
            bytesPerLine = Integer.parseInt(args[0]);
        }

        int i;
        for (i = 0; !BinaryStdIn.isEmpty(); i++) {
            if (bytesPerLine == 0) {
                BinaryStdIn.readChar();
                continue;
            }
            if (i == 0) StdOut.printf("");
            else if (i % bytesPerLine == 0) StdOut.printf("\n", i);
            else StdOut.print(" ");
            char c = BinaryStdIn.readChar();
            StdOut.printf("%02x", c & 0xff);
        }
        if (bytesPerLine != 0) StdOut.println();
        StdOut.println((i*8) + " bits");
    }
Esempio n. 22
0
    /**
     * Reads a sequence of 8-bit bytes from standard input; compresses them
     * using Huffman codes with an 8-bit alphabet; and writes the results
     * to standard output.
     */
    public static void compress() {
        // read the input
        String s = BinaryStdIn.readString();
        char[] input = s.toCharArray();

        // tabulate frequency counts
        int[] freq = new int[R];
        for (int i = 0; i < input.length; i++)
            freq[input[i]]++;

        // build Huffman trie
        Node root = buildTrie(freq);

        // build code table
        String[] st = new String[R];
        buildCode(st, root, "");

        // print trie for decoder
        writeTrie(root);

        // print number of bytes in original uncompressed message
        BinaryStdOut.write(input.length);

        // use Huffman code to encode input
        for (int i = 0; i < input.length; i++) {
            String code = st[input[i]];
            for (int j = 0; j < code.length(); j++) {
                if (code.charAt(j) == '0') {
                    BinaryStdOut.write(false);
                }
                else if (code.charAt(j) == '1') {
                    BinaryStdOut.write(true);
                }
                else throw new IllegalStateException("Illegal state");
            }
        }

        // close output stream
        BinaryStdOut.close();
    }
        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);
            }
        }
Esempio n. 24
0
 /**
  * Reads a sequence of bits from standard input; compresses
  * them using run-length coding with 8-bit run lengths; and writes the
  * results to standard output.
  */
 public static void compress() { 
     char run = 0; 
     boolean old = false;
     while (!BinaryStdIn.isEmpty()) { 
         boolean b = BinaryStdIn.readBoolean();
         if (b != old) {
             BinaryStdOut.write(run, LG_R);
             run = 1;
             old = !old;
         }
         else { 
             if (run == R-1) { 
                 BinaryStdOut.write(run, LG_R);
                 run = 0;
                 BinaryStdOut.write(run, LG_R);
             }
             run++;
         } 
     } 
     BinaryStdOut.write(run, LG_R);
     BinaryStdOut.close();
 }
Esempio n. 25
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);
        }
Esempio n. 26
0
    /**
     * Reads a sequence of bit encoded using LZW compression with
     * 12-bit codewords from standard input; expands them; and writes
     * the results to standard output.
     */
    public static void expand() {
        String[] st = new String[L];
        int i; // next available codeword value

        // initialize symbol table with all 1-character strings
        for (i = 0; i < R; i++)
            st[i] = "" + (char) i;
        st[i++] = "";                        // (unused) lookahead for EOF

        int codeword = BinaryStdIn.readInt(W);
        if (codeword == R) return;           // expanded message is empty string
        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.charAt(0);   // special case hack
            if (i < L) st[i++] = val + s.charAt(0);
            val = s;
        }
        BinaryStdOut.close();
    }
        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);
                    }
                }
            }
        }