#C14111. String Compression and Decompression

    ID: 43725 Type: Default 1000ms 256MiB

String Compression and Decompression

String Compression and Decompression

In this problem, you are required to implement two functionalities: compressing a given string and decompressing a string that was compressed using your compression algorithm. The compression function should replace sequences of consecutive identical characters with the character followed by the number of repetitions (if greater than 1). The decompression function should revert the compressed string back to its original form.

For example, given the string ( aaa,bbcddd ) as input for compression, the output should be ( a3b2cd3 ). Similarly, decompressing ( a3b2cd3 ) should yield ( aaabbcddd ).

Note: All numbers in the compressed string are in decimal notation and the formulas involved (if any) should be in ( \LaTeX ) format.

inputFormat

The input is provided via standard input (stdin) and consists of two lines:

  1. The first line is a mode indicator which is either "compress" or "decompress".
  2. The second line is the string ( S ) to be processed. ( S ) may be empty.

outputFormat

The program should output the processed string to standard output (stdout). If the mode is "compress", output the compressed string. If the mode is "decompress", output the decompressed string.## sample

compress
aaabbcddd
a3b2cd3