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
2013-06-16 19:30:40 Himanshu
@friends here is more test cases....
input:
1
1111111111
1231
output:
1111
911111
11123111

Last edit: 2013-06-21 18:41:10
2013-03-29 09:01:31 kamalesh
i/p
222133
o/p
32111123
bcs of this 1 WA
and @Anshul Gupta
o/p is
2a1b12a1b1
2013-02-17 04:03:05 nssuresh
Take care of len == 0 case as well
2013-02-02 19:16:31 Aditya Arun
My code works fine on ideone on all the example test cases, but the judge complains WA and sometimes SIGSEGV. Can anyone please help find my mistake.
http://ideone.com/aujufj
2012-07-02 09:58:07 saket diwakar
finallyyyyyyyyyyy got AC.:)phew.
@public_avenger
yes,your output is correct.


Last edit: 2012-07-02 09:59:39
2012-05-24 08:53:08 Ivan ©ego
Please use gets or something similar, don't use cin... cost me 3 WA
2012-05-22 09:08:15 PubLic_AvenGeR
wat will be the output for aaaaaaaaaabc??
9a1abc1 ??
and for aaaaaaaaaa
9a1a1??
2012-03-21 04:50:17 exor
Make sure you handle the case where the repetition exceeds 9. Cost me 4 WA :)
2011-09-21 07:56:23 Anshul Gupta
what will be the output for aabaab ??

2a1b12a1b1 or 2aab

Last edit: 2011-09-21 11:47:20
2011-05-17 10:58:07 albertg
Please, explain me the test cases?
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.