我正在为一个必须实现LZW压缩/解压缩的任务编写程序.我正在使用以下算法:
-压缩
w = NIL;
while ( read a character k )
{
if wk exists in the dictionary
w = wk;
else
add wk to the dictionary;
output the code for w;
w = k;
}
Run Code Online (Sandbox Code Playgroud)
-减压
read a character k;
output k;
w = k;
while ( read a character k )
/* k could be a character or a code. */
{
entry = dictionary entry for k;
output entry;
add w + entry[0] to dictionary;
w …Run Code Online (Sandbox Code Playgroud)