#K68067. Run-Length Encoding: Compression and Decompression
Run-Length Encoding: Compression and Decompression
Run-Length Encoding: Compression and Decompression
This problem requires you to implement the Run-Length Encoding (RLE) algorithm for both compression and decompression of strings.
Given a string s
, the compression function should convert it into its RLE representation, where consecutive occurrences of the same character are replaced by the character followed by the count. For example, the string aaabbbcccaaa is compressed to a3b3c3a3. Similarly, the decompression function should revert an RLE-compressed string back to its original form.
If we denote a group of consecutive identical characters as \(c\) repeated \(k\) times, then the compression works by replacing it with the pattern \(c\,k\). Formally, for a given block, the output is formatted as:
[ c,k ]
Your task is to read input from standard input (stdin) and write the result to standard output (stdout). The input consists of two lines: the first line is a command that is either "C" (for compression) or "D" (for decompression); the second line contains the corresponding string. Depending on the command, output the compressed or decompressed string.
inputFormat
The first line of input contains a single character command: C
for compress and D
for decompress.
The second line contains the string to be processed. The string can be empty or contain up to 104 characters.
outputFormat
Output a single line, which is the result of applying the specified operation on the input string. The result should be printed to standard output.
## sampleC
aaabbbcccaaa
a3b3c3a3