我一直在研究 LZW algorithm 最近用于数据压缩。我已经了解编码算法,但是,我无法理解将编码数据转换回原始形式的解码算法。
以下是取自 here 的解码器伪代码
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 个不同的字符串,即 entry、prevcode 和 currcode ?在我看来,一个应该是编码字符串,第二个应该是创建的输出字符串。那么第三根弦在那里做什么呢?
其次,我真的不明白代码倒数第二行(prevcode的翻译)+ch的用意。
谢谢。
最佳答案
如果您了解压缩部分,您也应该能够理解,要使解压工作,解压器需要从解压数据重建压缩表,就像在压缩期间构建的一样——否则进来的代码不是纯字符代码将毫无意义。这就是这些额外语句的作用。
关于algorithm - LZW数据解压算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18771559/