写一个 LZ77 压缩算法的代码? 用 C语言写
时间: 2023-03-13 07:26:52 浏览: 96
#include<stdio.h>
int match_length(char *string, int index, int size)
{
int length = 0;
while((string[index+length] == string[size+length]) && (length < index) && (string[index+length] != '\0'))
length++;
return length;
}
void lz77_compress(char *string, int window_size)
{
int i = 0, j = 0, length = 0;
while(string[i] != '\0')
{
length = match_length(string, i, j);
if(length > 0)
{
printf("(%d, %d, %c)", j, length, string[i+length]);
i += length;
j = i;
}
else
{
printf("(%d, 0, %c)", j, string[i]);
i++;
j++;
}
if(j >= window_size)
j = 0;
}
}
int main()
{
char *string = "ABABXABXABXAB";
int window_size = 5;
lz77_compress(string, window_size);
return 0;
}
阅读全文