关于哈弗曼译码算法 注意是译码  给定下表  然后输入一段01码  如何译码???  给个思路  或者代码 那就更好了!!
A:Weight = 64  Code = 1110
B:Weight = 13  Code = 110000
C:Weight = 22  Code = 01000
D:Weight = 32  Code = 11110
E:Weight = 103 Code = 100
F:Weight = 21  Code = 00011
G:Weight = 15  Code = 110001
H:Weight = 47  Code = 0101
I:Weight = 57  Code = 1010
J:Weight = 1   Code = 000001000
K:Weight = 5   Code = 0000011
L:Weight = 32  Code = 11111
M:Weight = 20  Code = 00010
N:Weight = 57  Code = 1011
O:Weight = 63  Code = 1101
P:Weight = 15  Code = 110010
Q:Weight = 1   Code = 000001001
R:Weight = 48  Code = 0110
S:Weight = 51  Code = 0111
T:Weight = 80  Code = 001
U:Weight = 23  Code = 01001
V:Weight = 8   Code = 000000
W:Weight = 18  Code = 00001
X:Weight = 1   Code = 000001010
Y:Weight = 16  Code = 110011
Z:Weight = 1   Code = 000001011

解决方案 »

  1.   

    给lz一个思路:有限状态机。
    思路很简单,但是这个程序不怎么好写(貌似有限状态机的程序都很恶~主要是枚举状态让人头大- -!)。
    懒了,lz自己写吧。
      

  2.   

    lz还是别等了,这个程序不是一时半会儿写的出来的。
    26个字母应该有51个状态,光枚举这些状态就得半天。
    周末有空的话给lz写一个,现在是来不及了。
      

  3.   

    狠狠心,写了一个,困疯了,睡了。/**
     * 有限状态机实现哈弗曼编码的译码
     * 
     * @author Aeolus
     * 
     */
    public class Halfman
    {
    private char[] code = null;

    public Halfman(String strCode)
    {
    code = strCode.toCharArray();
    }

    public String decode()
    {
    StringBuilder sb = new StringBuilder("");
    int status = 0;
    for (char ch : code)
    {
    switch (status)
    {
    default:
    return "An unknown error occured!";
    case 0: // ""
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    status = 1;
    break;
    case '1':
    status = 2;
    break;
    }
    break;
    case 1: // 0
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    status = 3;
    break;
    case '1':
    status = 4;
    break;
    }
    break;
    case 2: // 1
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    status = 5;
    break;
    case '1':
    status = 6;
    break;
    }
    break;
    case 3: // 00
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    status = 7;
    break;
    case '1':
    sb.append('T');
    status = 0;
    break;
    }
    break;
    case 4: // 01
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    status = 8;
    break;
    case '1':
    status = 9;
    break;
    }
    break;
    case 5: // 10
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    sb.append('E');
    status = 0;
    break;
    case '1':
    status = 10;
    break;
    }
    break;
    case 6: // 11
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    status = 11;
    break;
    case '1':
    status = 12;
    break;
    }
    break;
    case 7: // 000
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    status = 13;
    break;
    case '1':
    status = 14;
    break;
    }
    break;
    case 8: // 010
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    status = 15;
    break;
    case '1':
    sb.append('H');
    status = 0;
    break;
    }
    break;
    case 9: // 011
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    sb.append('R');
    status = 0;
    break;
    case '1':
    sb.append('S');
    status = 0;
    break;
    }
    break;
    case 10: // 101
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    sb.append('I');
    status = 0;
    break;
    case '1':
    sb.append('N');
    status = 0;
    break;
    }
    break;
    case 11: // 110
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    status = 16;
    break;
    case '1':
    sb.append('O');
    status = 0;
    break;
    }
    break;
    case 12: // 111
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    sb.append('A');
    status = 0;
    break;
    case '1':
    status = 17;
    break;
    }
    break;
    case 13: // 0000
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    status = 18;
    break;
    case '1':
    sb.append('W');
    status = 0;
    break;
    }
    break;
    case 14: // 0001
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    sb.append('M');
    status = 0;
    break;
    case '1':
    sb.append('F');
    status = 0;
    break;
    }
    break;
    case 15: // 0100
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    sb.append('C');
    status = 0;
    break;
    case '1':
    sb.append('U');
    status = 0;
    break;
    }
    break;
    case 16: // 1100
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    status = 19;
    break;
    case '1':
    status = 20;
    break;
    }
    break;
    case 17: // 1111
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    sb.append('D');
    status = 0;
    break;
    case '1':
    sb.append('L');
    status = 0;
    break;
    }
    break;
    case 18: // 00000
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    sb.append('V');
    status = 0;
    break;
    case '1':
    status = 21;
    break;
    }
    break;
    case 19: // 11000
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    sb.append('B');
    status = 0;
    break;
    case '1':
    sb.append('G');
    status = 0;
    break;
    }
    break;
    case 20: // 11001
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    sb.append('P');
    status = 0;
    break;
    case '1':
    sb.append('Y');
    status = 0;
    break;
    }
    break;
    case 21: // 000001
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    status = 22;
    break;
    case '1':
    sb.append('K');
    status = 0;
    break;
    }
    break;
    case 22: // 0000010
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    status = 23;
    break;
    case '1':
    status = 24;
    break;
    }
    break;
    case 23: // 00000100
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    sb.append('J');
    status = 0;
    break;
    case '1':
    sb.append('Q');
    status = 0;
    break;
    }
    break;
    case 24: // 00000101
    switch (ch)
    {
    default:
    return "There is a charactor other than '0' and '1'!";
    case '0':
    sb.append('X');
    status = 0;
    break;
    case '1':
    sb.append('Z');
    status = 0;
    break;
    }
    break;
    }
    }

    return sb.toString();
    }

    public static void main(String[] args)
    {
    // encode of "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    String encode = "1110110000010001111010000011110001010110100000010000000011111110001010111101110010000001001011001110010100100000000001000001010110011000001011";

    Halfman halfman = new Halfman(encode);
    System.out.println(halfman.decode());
    }
    }