0

我最近一直在研究用于数据压缩的LZW 算法。我已经了解编码算法,但是,我无法理解将编码数据转换回其原始形式的解码算法。

以下是从这里获取的解码器的伪代码

string entry;
char ch;
int prevcode, currcode;
...

prevcode = read in a code;
decode/output prevcode;
while (there is still data to read)
{
  currcode = read in a code;
  entry = translation of currcode from dictionary;
  output entry;
  ch = first char of entry;
  add ((translation of prevcode)+ch) to dictionary;
  prevcode = currcode;
}

我正在寻找此代码的分步说明。

编辑:我不明白的是:为什么我们有 3 个不同的字符串,即entryprevcodecurrcode?在我看来,一个应该是编码字符串,第二个应该是创建的输出字符串。那么第三个字符串在那里做什么呢?

其次,我真的不明白代码倒数第二行中(prevcode的翻译)+ch的目的。

谢谢。

4

1 回答 1

2

如果您了解压缩部分,您还应该能够理解,要使解压缩工作,解压缩器需要从解压缩的数据中重建压缩表,就像在压缩期间构建的一样 - 否则进来的代码不是纯字符代码将毫无意义。这就是这些额外语句的作用。

于 2013-09-12T22:33:40.773 回答