如何进行游程编码?
我有一个很长的字符串,例如它可能是“aaaaaabbccc”。需要将其表示为“a6b2c3”。最好的方法是什么?我可以通过比较字符和递增计数,然后在一次传递中使用两个索引替换数组中的计数,以线性时间完成此操作。你们能想出比这更好的办法吗?任何编码技术都适用于此吗?
I have a long string for example it could be "aaaaaabbccc". Need to represent it as "a6b2c3". What's the best way to do this? I could do this in linear time by comparing characters and incrementing counts and then replacing the counts in the array, using two indexes in one pass. Can you guys think of a better way than this? Are any of the encoding techniques going to work here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
常见的解决方案是 RLE - 运行长度编码,维基百科文章有示例实现代码。
The common solution for this is RLE - Run-length encoding, the Wikipedia article has sample implementation code.
我认为没有更快的方法来解决它。
非正式地,您可以认为次线性复杂度意味着进行的比较次数少于要压缩的字符串中的字符数。但是,通过如此小的比较,您无法确定某些字符,您无法知道它们包含什么,因为您没有足够的信息。这意味着您无法获得无损< /strong> 压缩。
I don't think there is a faster way to solve it.
Informally you can think that a sub-linear complexity implies to do less comparisons that the number of the characters in the string you want to compress. But with a number of comparisons such small you can't be sure of some character, you can't know what they contain because you don't have enough information.. this means that you can't obtain a loseless compression.
我想你在问,“有没有比线性更好的方法来进行游程编码”?如果是这样,答案是否定的。
I think you're asking, "Is there a better than linear way to do run-length encoding"? If so, the answer is no.
不过,我已经实现了字节编码。希望有帮助。
I have a implemented an encoding for bytes though. Hope it helps.