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
2011-05-14 07:20:46 cegprakash
@Aditya:

it will be "4space\n"

to all:
if the input is a blank line just print an empty line

the string length is less than 51
2011-02-19 19:18:18 Aditya A R
what will b the output for
"spacespacespacespace\n"
will it be "4space\n" or empty line???


Last edit: 2011-05-14 07:18:55
2010-10-02 11:40:08 evans
can anyone tell me how to take input till end of file
2010-06-25 09:12:16 Blue
input has empty lines too output empty line

Last edit: 2010-06-25 09:13:32
2010-04-20 16:59:57 :(){ :|: & };:

This is actually a nice shortening task :-)
2010-04-02 16:16:46 LeppyR64
@aslam
1111
2010-02-14 09:16:35 Anuj Agrawal
input 1
output ?
2010-02-03 06:04:55 Tarun Kumar
@ Dark lord
91111 21

Last edit: 2010-02-03 06:09:45
2009-12-30 21:56:27 Dark Lord
Whats the answer for 1111111111 2

Last edit: 2009-12-30 22:00:44
2009-11-26 10:15:18 c4758749
is string getting accepted at all ???
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.