【问题标题】:Decoding bit to find string - Requesting Huffman technique解码位以查找字符串 - 请求霍夫曼技术
【发布时间】:2017-11-26 15:49:04
【问题描述】:

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

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;

    }

但对于大多数测试用例来说,它似乎都失败了。谁能指出我做的错误?并且无论如何也要提高上述代码的性能?

还有什么方法可以更快地解码?我的意思是编码字符串的长度可以是 7000,代码最多可以是 100。执行这些需要花费很多时间。我研究了霍夫曼解码。可以在这里用来固定吗?

提前致谢

【问题讨论】:

    标签: c# .net bit decoding huffman-code


    【解决方案1】:

    如果我运行您的代码,请提供如下参数:

    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;
        }
    }
    

    我删除了if,因为我不明白它:P 并注意到i &lt; encoded.Length 而不是i &lt;= encoded.Length,否则会导致异常。

    关于您的实施的一些注意事项:

    • 您可以使用Dictionary&lt;string, string&gt; 和dictionary.TryGetValue(...) 代替Hashtable 来测试它是否包含某个字符串
    • 如果“代码”始终具有相同的长度,您可以简化代码,而不必分别遍历每个字符
    • 将codes[i].Split()的结果保存到变量中,而不是重复多次

    【讨论】:

      【解决方案2】:

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

      static string decode(string codes, string encoded)
      

      代码:

      static void Main(string[] args)
      {
          string codes = "a 100100 b 100101 c 110001 d 100000 [newline] 111111 p 111110 q 000001";
          string encoded = "111110000001100100111111100101110001111110";
      
          Console.WriteLine("");
          try
          {
              string decoded = decode(codes, encoded);
      
              Console.WriteLine("codes:");
              Console.WriteLine(codes);
              Console.WriteLine("");
              Console.WriteLine("encoded: ");
              Console.WriteLine(encoded);
              Console.WriteLine("");
              Console.WriteLine("decoded: ");
              Console.WriteLine(decoded);
          }
          catch(Exception ex)
          {
              Console.WriteLine("Decoding Failed.");
              Console.WriteLine("Reason: " + ex.Message);
          }
      
          Console.WriteLine("");
          Console.Write("Press [Enter] to exit...");
          Console.ReadLine();
      }
      
      static string decode(string codes, string encoded)
      {
          StringBuilder decoded = new StringBuilder();
      
          List<Tuple<string, string>> codePairs = new List<Tuple<string, string>>();
          string[] codeValues = codes.Trim().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
          if (codeValues.Length % 2 != 0)
          {
              throw new Exception("Invalid number of code pairs!");
          }
      
          for (int i = 0; i < codeValues.Length - 1; i = i + 2)
          {
              if (codeValues[i].ToLower() == "[newline]")
              {
                  codePairs.Add(new Tuple<string, string>(Environment.NewLine, codeValues[i + 1]));
              }
              else
              {
                  codePairs.Add(new Tuple<string, string>(codeValues[i], codeValues[i + 1]));
              }
          }
      
          Boolean matchFound;
          while(encoded.Length > 0)
          {
              matchFound = false;
              foreach(Tuple<string, string> codePair in codePairs)
              {
                  if (encoded.StartsWith(codePair.Item2))
                  {
                      encoded = encoded.Remove(0, codePair.Item2.Length);
                      decoded.Append(codePair.Item1);
                      matchFound = true;
                      break;
                  }
              }
              if (!matchFound)
              {
                  throw new Exception("Invalid encoded string!  No matching code found.");
              }
          }
      
          return decoded.ToString();
      }
      

      产生以下输出:

      codes:
      a 100100 b 100101 c 110001 d 100000 [newline] 111111 p 111110 q 000001
      
      encoded:
      111110000001100100111111100101110001111110
      
      decoded:
      pqa
      bcp
      
      Press [Enter] to exit...
      

      【讨论】:

      • 有什么方法可以更快地解码?我的意思是编码字符串的长度可以是 7000,代码可以达到 100。执行这些需要花费很多时间。我研究了霍夫曼解码。可以在这里用来固定吗?
      • 你能把长度为 7000 的样本编码字符串和最多 100 个代码放在 PasteBin 上,这样我就可以测试速度了吗?
      • 有什么进展吗?我有一些想法,但希望能用更长的样本数据进行测试。
      • 我无法生成这些大数据的输入 :(
      猜你喜欢
      • 2017-08-18
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多