您能给我一些解释LZ77算法吗?

发布于 2025-01-18 10:48:31 字数 330 浏览 3 评论 0原文

我正在尝试和我的朋友学习LZ77算法,有些情况让我们感到困惑。

例如)

初始化

  • 搜索缓冲区大小:7
  • 前瞻缓冲区大小:8
  • 原始字符串:abcabbcabbcabca
  • 当前 窗口:abcabbc 视图:abbcabca

我认为 LLD 元组是: 字面意思:'a' 长度:4 距离:4

我朋友认为 LLD 元组是: 字面意思:“c” 长度:6 距离:4

我认为查找最长匹配字符串的算法仅检查搜索缓冲区,但他说查找匹配字符串没有限制。

谁的答案是正确的?

I'm trying to learn LZ77 algorithm with my friend, and some case give us a confusion.

for example)

init

  • search buffer size: 7
  • look-ahead buffer size: 8
  • original string: abcabbcabbcabca
  • current
    window: abcabbc
    view: abbcabca

What I thought the LLD tuple is :
Literal: 'a'
Length: 4
Distance: 4

What my friend thought the LLD tuple is :
Literal: 'c'
Length: 6
Distance: 4

I thought the algorithm to find longest match string only checks in search buffer, but he says there is no limit to find match string.

Who's answer is correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如若梦似彩虹 2025-01-25 10:48:31

我无法给你一个明确的答案,但我可以证明 LZ77 没有理由不支持“过长”的元组。


我相信您在问元组的长度分量是否可以大于其距离分量。

换句话说,我相信您是在问将生成以下哪个元组流:

  1. ( Length: 0, Distance: 0, Char: 'a' )
  2. (0,0,'b')
  3. (0,0, 'c')
  4. (2,3,'b')
  5. (4,4,'c')[1] 长度上限为距离。
  6. (2,4,'c')
  7. (0,0,'a')

  1. (0,0,'a')
  2. (0,0,'b')
  3. (0,0,'c')
  4. (2,3 ,'b')
  5. (7,4,'c')[2] 长度大于距离。
  6. (0,0,'a')

显然,后者会产生更好的压缩效果。

我无法给你一个明确的答案,但我可以证明解码器可以像编码器生成元组一样轻松地处理“过长”的元组。因此,LZ77 没有理由不支持他们。


重建第一个流的输入

  1. (0,0,'a'): "" + "a" ⇒ "a"
  2. (0,0,'b'): "" + "b" ⇒ "ab"
  3. (0,0,'c'): "" + "c" ⇒ "abc"
  4. (2,3,'b'): "ab" + "b" ⇒ "abcabb"
  5. (4,4, 'c'): “cabb”+“c”⇒“abcabbcabbc”
  6. (2,4,'c'):“ab”+“c”⇒“abcabbcabbcabc”
  7. (0,0,'a'):“”+“a”⇒“ abcabbcabbcabca”

简单。


重建第二个流的输入

  1. (0,0,'a'): "" + "a" ⇒ "a"

  2. (0,0,'b'): "" + "b" ⇒ "ab"

  3. (0,0,'c'): "" + "c" ⇒ "abc"

  4. (2,3,'b'): "ab" + "b" ⇒ "abcabb"

  5. (7,4,'c')

    到目前为止,我们已经生成了 abcabb。该元组表示接下来的 7 个字符从 abcabb 末尾的 4 个字符开始。

    abccabb _ _ _ _ _ _ _ c
          | | | | ^^^^^^^^
          | | | | | | | | | | |
          +-)-)-)-+ | | | ? ? ?
            +-)-)---+ | |
              +-)-----+ |
                +--------+
    

    我们缺少三个角色!我们是吗?如果都是一个缓冲区并且我们从左到右复制,我们就必须继续下去。

    <代码> +--------+
                    +-)-----+ |
                  +-)-)---+ | |
                  | | | | | |
                  | | |维维
    accabb _ _ _ _ _ _ _ c
          | | | | ^^^^
          | | | | | | | |
          +-)-)-)-+ | | |
            +-)-)---+ | |
              +-)-----+ |
                +--------+
    

    所以这不需要额外的努力就可以工作!

    (7,4,'c'): "cabbcab" + "c" ⇒ "abcabbcabbcabc"

  6. (0,0,'a'): "" + "a" ⇒ "abcabbcabbcabca"


  1. 你错误地说道 (4,4,'a')。
  2. 你错误地说了(6,4,'c')。

I can't give you a definitive answer, but I can show there's no reason LZ77 couldn't support "overly long" tuples.


I believe you're asking if the length component of a tuple can be larger than its distance component.

In other words, I believe you're asking which of the following streams of tuples would be produced:

  1. ( Length: 0, Distance: 0, Char: 'a' )
  2. (0,0,'b')
  3. (0,0,'c')
  4. (2,3,'b')
  5. (4,4,'c')[1] Length capped to distance.
  6. (2,4,'c')
  7. (0,0,'a')

or

  1. (0,0,'a')
  2. (0,0,'b')
  3. (0,0,'c')
  4. (2,3,'b')
  5. (7,4,'c')[2] Length larger than distance.
  6. (0,0,'a')

Obviously, the latter would produce better compression.

I can't give you a definitive answer, but I can show that the decoder can handle "overly long" tuples just as easily as the encoder can produce them. As such, there's no reason LZ77 couldn't support them.


Reconstructing the input from the first stream

  1. (0,0,'a'): "" + "a" ⇒ "a"
  2. (0,0,'b'): "" + "b" ⇒ "ab"
  3. (0,0,'c'): "" + "c" ⇒ "abc"
  4. (2,3,'b'): "ab" + "b" ⇒ "abcabb"
  5. (4,4,'c'): "cabb" + "c" ⇒ "abcabbcabbc"
  6. (2,4,'c'): "ab" + "c" ⇒ "abcabbcabbcabc"
  7. (0,0,'a'): "" + "a" ⇒ "abcabbcabbcabca"

Simple.


Reconstructing the input from the second stream

  1. (0,0,'a'): "" + "a" ⇒ "a"

  2. (0,0,'b'): "" + "b" ⇒ "ab"

  3. (0,0,'c'): "" + "c" ⇒ "abc"

  4. (2,3,'b'): "ab" + "b" ⇒ "abcabb"

  5. (7,4,'c')

    So far, we've produced abcabb. The tuple says the next 7 characters start 4 characters from the end of abcabb.

    a b c c a b b _ _ _ _ _ _ _ c
          | | | | ^ ^ ^ ^ ^ ^ ^
          | | | | | | | | | | |
          +-)-)-)-+ | | | ? ? ?
            +-)-)---+ | |
              +-)-----+ |
                +-------+
    

    We're missing three characters! Are are we? If it's all one buffer and we copy from left to right, we just have to keep going.

                      +-------+
                    +-)-----+ |
                  +-)-)---+ | |
                  | | |   | | |
                  | | |   v v v
    a b c c a b b _ _ _ _ _ _ _ c
          | | | | ^ ^ ^ ^
          | | | | | | | |
          +-)-)-)-+ | | |
            +-)-)---+ | |
              +-)-----+ |
                +-------+
    

    So this works with no extra effort!

    (7,4,'c'): "cabbcab" + "c" ⇒ "abcabbcabbcabc"

  6. (0,0,'a'): "" + "a" ⇒ "abcabbcabbcabca"


  1. You incorrectly said (4,4,'a').
  2. You incorrectly said (6,4,'c').
夜声 2025-01-25 10:48:31

你的朋友是对的。一个简单的示例是,LZ77使用距离1匹配完成了运行长度编码,而长度为n-1 对于同一字节的 n 副本的运行。 (字节的第一次出现是字面意思。)

Your friend is right. A simpler example is that LZ77 accomplishes run-length coding with a distance 1 match, with length n-1 for a run of n copies of the same byte. (The first occurrence of the byte is a literal.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文