java Run-length encoding

后端 未结 5 1303
臣服心动
臣服心动 2020-12-06 08:38

I have no idea how to start my assignment.

We got to make a Run-length encoding program,

for example, the users enters this string:

aaaaPPPrrrrr

5条回答
  •  再見小時候
    2020-12-06 09:22

    Hopefully this will get you started on your assignment:

    The fundamental idea behind run-length encoding is that consecutively occurring tokens like aaaa can be replaced by a shorter form 4a (meaning "the following four characters are an 'a'"). This type of encoding was used in the early days of computer graphics to save space when storing an image. Back then, video cards supported a small number of colors and images commonly had the same color all in a row for significant portions of the image)

    You can read up on it in detail on Wikipedia

    http://en.wikipedia.org/wiki/Run-length_encoding

    In order to run-length encode a string, you can loop through the characters in the input string. Have a counter that counts how many times you have seen the same character in a row. When you then see a different character, output the value of the counter and then the character you have been counting. If the value of the counter is 1 (meaning you only saw one of those characters in a row) skip outputting the counter.

提交回复
热议问题