#K59052. String Compression and Decompression
String Compression and Decompression
String Compression and Decompression
This problem requires you to implement two functions: one to compress a string and another to decompress it.
In compression, consecutive occurrences of the same character are replaced by the character followed by the count of its occurrences (in LaTeX: $\texttt{char}{\texttt{count}}$). For example, the string "aaabbcdddd" is compressed to "a3b2c1d4". In decompression, the process is reversed, recovering the original string from its compressed form.
You will be given multiple test cases. Each test case has two inputs: the first indicates the operation (compress
or decompress
), and the second is the string to be processed. Your program should process each test case and print the result on a new line.
inputFormat
The first line contains an integer \(T\), the number of test cases. Each test case consists of two lines:
- The first line is a string that is either
compress
ordecompress
. - The second line is the string to be processed. It may be empty.
outputFormat
For each test case, output a single line containing the result of the operation. If the operation is compress
, output the compressed string; if it is decompress
, output the original string.
4
compress
aaabbcdddd
compress
abc
decompress
a3b2c1d4
decompress
a1b1c1
a3b2c1d4
a1b1c1
aaabbcdddd
abc
</p>