#C13038. Run-Length Encoding Compression and Decompression
Run-Length Encoding Compression and Decompression
Run-Length Encoding Compression and Decompression
This problem challenges you to implement a simple run-length encoding algorithm both for compressing and decompressing strings.
Compression: If a character in the string appears consecutively, replace the sequence with the character followed by the count (only if the count is greater than 1). Otherwise, leave the character as-is. For example, the string aaabbccaaa compresses to a3b2c2a3.
Decompression: Reverse the compression process. If a character is immediately followed by a number (in latex format, e.g., $3$), then that character is repeated that many times.
Your program needs to support both operations based on the input provided.
inputFormat
The input is given via stdin and consists of two lines:
-
The first line contains a single character indicating the operation:
C
for compressing the string using run-length encoding.D
for decompressing a run-length encoded string back to its original form.
-
The second line contains the string to be processed.
outputFormat
Output the resulting string to stdout after performing the specified operation.## sample
C
aaabbccaaa
a3b2c2a3