#K36077. String Compression and Decompression
String Compression and Decompression
String Compression and Decompression
You are required to implement a program that performs compression
and decompression
on strings. The compression algorithm replaces each contiguous sequence of the same character with the character followed by the count of its repetitions. The decompression algorithm reverses this process.
Specifically, given an input string s, the compressed version is obtained by replacing any sequence of consecutive identical characters \( c \) repeated \( n \) times with cn
in the output. The decompression reverses this by expanding each occurrence of a character and its count into a sequence of that character repeated count times.
For example:
- Compression: "AAABBBCCAAA" becomes "A3B3C2A3".
- Decompression: "A3B3C2A3" becomes "AAABBBCCAAA".
The program input is taken from stdin and the output should be written to stdout.
inputFormat
The input consists of two lines:
- The first line contains a single character which is either
C
for compression orD
for decompression. - The second line contains the string s on which the operation is to be performed. The string may be empty.
Note: For compression, the input string will contain uppercase letters only. For decompression, the string is guaranteed to be in the valid compressed format.
outputFormat
Output a single line to stdout representing the result of compressing or decompressing the input string based on the provided operation mode.
## sampleC
AAABBBCCAAA
A3B3C2A3