ENCONDIN - Run Length Encoding

Your task is to write a program that performs a simple form of run-length encoding, as described by the rules below.

Any sequence of between 2 to 9 identical characters is encoded by two characters. The first character is the length of the sequence, represented by one of the characters 2 through 9. The second character is the value of the repeated character. A sequence of more than 9 identical characters is dealt with by first encoding 9 characters, then the remaining ones.

Any sequence of characters that does not contain consecutive repetitions of any characters is represented by a 1 character followed by the sequence of characters, terminated with another 1. If a 1 appears as part of the sequence, it is escaped with a 1, thus two 1 characters are output.

Input Specification

The input consists of letters (both upper- and lower-case), digits, spaces, and punctuation. Every line is terminated with a newline character and no other characters appear in the input.

Output Specification

Each line in the input is encoded separately as described above. The newline at the end of each line is not encoded, but is passed directly to the output.

Sample Input

AAAAAABCCCC
12344

Sample Output

6A1B14C
11123124

Added by:Wanderley Guimarăes
Date:2007-09-19
Time limit:0.303s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO
Resource:University of Ulm Local Contest 2004

hide comments
2009-11-12 21:28:11 nthrgeek
@ Tony Beta Lambda

Yes,Input end with EOF.
Just put the input procedure within a loop.

Maximum input size of 10001 works fine here.
2009-11-12 21:25:33 nthrgeek
@Nikkolloz

Input:
2314
Output:
1231141



Last edit: 2009-11-12 21:29:00
2009-07-02 10:10:03 kunal
what is the answer for 23114 ?
2009-06-29 15:09:52 Ankul Garg
@Kunal 123121141

Last edit: 2009-10-13 17:24:56
2009-05-18 08:50:05 m7nika
what is the answer for 2314 ?
2009-05-05 10:40:12 Tony Beta Lambda
How to specify that the input is end? Does it end with an EOF mark or an empty line? Furthermore, is there any constraints on the length of string?
2009-02-21 19:20:15 David Pìgøímek (Davpe)
I think, there should be "If a 1 appears as part of THAT sequence, it is escaped..." to be more clarified. It would help me ;)
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.