编程问题-传真压缩
我正准备通过完成过去竞赛中的问题来参加计算机科学竞赛。 其中大多数都很简单,但这一个却困扰着我……看起来很简单,但我就是做不到。
如果您有一串 1 和 0:
100111010001111100101010
将其作为输入然后输出的代码是什么:
1:1 2:0 3:1 1:0 1:1 3:0 5:1 2:0 1:1 1:0 1:1 1:0
其中每个冒号左侧的数字是冒号后面的数字出现的次数。
因此,另一个例子...输入:
1100011
将输出:
2:1 3:0 2:1
根据问题,这类似于用于压缩传真传输的算法。
java 中的答案是最好的,但我真正寻找的是伪代码,甚至是关于如何做到这一点的想法。
提前致谢。
I'm preparing to go to a computer science contest by completing problems from past contests. Most of them are pretty easy, but this one is bugging me...it seems simple but I'm just not being able to do it.
If you have a string of ones and zeros:
100111010001111100101010
What would be the code to take that as an input and then output this:
1:1 2:0 3:1 1:0 1:1 3:0 5:1 2:0 1:1 1:0 1:1 1:0
Where the digit to the left of each colon is the number of times the digit after the colon appears.
So, another example...inputting:
1100011
Would output:
2:1 3:0 2:1
According to the problem this is similar to the algorithm used to compress fax transmissions.
An answer in java would be best, but all I'm really looking for is pseudocode or even thoughts on how to do it.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这称为运行长度编码 (RLE),用于许多事物(例如 Windows 位图文件格式)以提供非常基本的压缩(特别是如果原始文件包含大量重复值(例如位图或传真) )包含长串相同颜色)。
This is called Run-Length-Encoding (RLE) and is used in a number of things (such as the Windows Bitmap file-format) to provide very basic compression (especially if the original includes lots of repeated values (like a bitmap or fax) containing a long run of the same colour).
试想一下:你为什么要为右边的数字烦恼呢? 它总是会在 1 和 0 之间交替,不是吗,所以假设它以 1 开头,如果实际序列以 0 开头,则编码初始 0。换句话说,你最终会得到:
1 2 3 1 1 3 5 2 1 1 1 1
但基本上你需要跟踪“我当前在看什么?” 以及“我见过多少个”? 如果发生变化,请写下您一直在查看的内容和计数,然后将“我正在查看的内容”更新为新值,并将计数更新为 1,然后继续。 不要忘记在数据末尾也写出最后一个值。
(我没有给出伪代码或 Java,因为我认为通过小提示你会学到比工作代码更多的东西。如果你需要进一步的提示,尽管说。)
Just as a thought: why would you bother with the number on the right? It will always alternate between 1 and 0 won't it, so just assume it starts with 1 and encode an initial 0 if the actual sequence starts with 0. In other words, you'd end up with:
1 2 3 1 1 3 5 2 1 1 1 1
But basically you need to keep track of "what am I currently looking at?" and "how many of them have I seen"? If it changes, write out what you've been looking at and the count, and then update "what I'm looking at" to the new value and the count to 1, then keep going. Don't forget to write out the last value at the end of the data as well.
(I haven't given pseudocode or Java as I think you'll learn more by taking small hints than being presented with working code. If you need further hints though, just say.)
以下是一些想法:
使用上述方法处理第一个字节中的 8 位。 然后重复此操作以处理下一个字节中的接下来的 8 位。
一些伪代码可能如下所示:
当您编写代码时,不要忘记使用各种输入数据测试它,包括特殊情况(例如,包括一个字节中的所有位)具有相同的值)。
Here are some thoughts:
Use the above methods, to process the 8 bits in the first byte. Then repeat this to handle the next 8 bits, in the next byte.
Some pseudo-code may be something like the following:
When you write your code, don't forget to also test it using various input data, including special cases (including for example all the bits in a byte having the same value).