C# 解码位以找到请求字符串的哈夫曼技术

C# 解码位以找到请求字符串的哈夫曼技术,c#,.net,bit,decoding,huffman-code,C#,.net,Bit,Decoding,Huffman Code,例如,我正在尝试构建一个解码器 a 100100 b 100101 c 110001 d 100000 [newline] 111111 p 111110 q 000001 编码位: 111110000001100100111111100101110001111110 这应该给 pqa bcp 我尝试的代码: static string decode(string[]

例如,我正在尝试构建一个解码器

a           100100
b           100101
c           110001
d           100000
[newline]   111111
p           111110
q           000001
编码位:

111110000001100100111111100101110001111110
这应该给

pqa
bcp
我尝试的代码:

static string decode(string[] codes, string encoded) {
            string finalString = string.Empty;
            string temp = string.Empty;
            System.Collections.Hashtable hashtable = new System.Collections.Hashtable();

            for (int i = 0; i < codes.Length; i++)
            {
                if (codes[i].Split()[0].Contains("new"))
                {
                    var newLine = Environment.NewLine;
                    hashtable.Add(codes[i].Split()[1], newLine);
                    continue;
                }
                hashtable.Add(codes[i].Split()[1], codes[i].Split()[0]);
            }

            for (int i = 0; i <= encoded.Length; i++)
            {
                if (codes.Length!=finalString.Length)
                {
                    temp = temp + encoded[i];
                    if (hashtable[temp] != null)
                    {
                        finalString = finalString + hashtable[temp].ToString();
                        encoded = encoded.Remove(0, temp.Length);
                        temp = string.Empty;
                        i = -1;
                    }
                }
            }
            return finalString;

    }
静态字符串解码(字符串[]代码,字符串编码){
string finalString=string.Empty;
字符串温度=字符串为空;
System.Collections.Hashtable Hashtable=新的System.Collections.Hashtable();
对于(int i=0;i对于(int i=0;i,如果我运行代码,请提供以下参数:

string[] str = new string[] { "a 100100","b 100101","c 110001","d 100000","[newline] 111111","p 111110","q 000001" };
string encoded = @"111110000001100100111111100101110001111110";
string res = decode(str, encoded);
结果是“pqa\r\nbc”
。这离期望的结果不远,只是错过了最后一个字符

您可以通过将第二个
for
循环简化为:

for (int i = 0; i < encoded.Length; i++)
{
    temp = temp + encoded[i];
    if (hashtable[temp] != null)
    {
        finalString = finalString + hashtable[temp].ToString();
        encoded = encoded.Remove(0, temp.Length);
        temp = string.Empty;
        i = -1;
    }
}
for(int i=0;i

我删除了
if
,因为我不理解它:p,请注意
I
,而不是
I,如果我运行代码,请提供以下参数:

string[] str = new string[] { "a 100100","b 100101","c 110001","d 100000","[newline] 111111","p 111110","q 000001" };
string encoded = @"111110000001100100111111100101110001111110";
string res = decode(str, encoded);
结果是“pqa\r\nbc”
。这离期望的结果不远,只是错过了最后一个字符

您可以通过将第二个
for
循环简化为:

for (int i = 0; i < encoded.Length; i++)
{
    temp = temp + encoded[i];
    if (hashtable[temp] != null)
    {
        finalString = finalString + hashtable[temp].ToString();
        encoded = encoded.Remove(0, temp.Length);
        temp = string.Empty;
        i = -1;
    }
}
for(int i=0;i

我删除了
if
,因为我不理解它:p,请注意
I
而不是
I这是我的看法。请注意,我更改了
decode()
的签名以接收字符串而不是字符串数组:

static string decode(string codes, string encoded)
代码:


以下是我的看法。请注意,我将
decode()
的签名更改为接收字符串而不是字符串数组:

static string decode(string codes, string encoded)
代码:


有什么方法可以更快地解码吗?我的意思是编码字符串的长度可以是7000,代码最多可以是100。执行这些代码需要很多时间。我研究了霍夫曼解码。这可以用来加快速度吗?你能把长度7000的编码字符串样本和最多100个代码放在PasteBin上,这样我就可以测试速度了吗?有什么进展吗?我有一些想法,但希望能够测试更长的样本数据。我无法生成这些大数据的输入:(有什么方法可以更快地解码吗?我的意思是编码字符串的长度可以是7000,代码最多可以是100。执行这些代码需要很多时间。我研究了霍夫曼解码。这可以用来加快速度吗?你能把长度7000的样本编码字符串和最多100个代码放在PasteBin上,这样我就可以测试速度了吗?有什么进展吗?我有一些想法,但希望能够测试更长的样本数据。我无法生成这些大数据的输入:(