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

hide comments
kishan_: 2017-10-27 21:57:04

Please remember if len=0 then print a blank line, It caused me 4WA and lot of time waste.

vengatesh15: 2017-01-21 17:02:50

AC in 1 go

prakash: 2017-01-04 18:39:33

simple one read input as while(gets(str))

Last edit: 2017-01-04 18:40:30
coolcoderext: 2015-05-23 12:33:08

Don't use scanf to input the string as the input string can also contain spaces.

Divyank Duvedi: 2014-05-20 08:39:13

What is the maximum string length limit

Rishav Goyal: 2014-03-30 16:32:41

more like precision...!!!

miodziu: 2014-01-06 16:00:42

@Abhimanyu:
The answer for "11" is "21"
The answer for "1111111111" (10 times '1') is "911111":
"91" - the first 9 chars '1'
"1" - start of block
"11" - character '1' with escaped character
"1" - end of block

Abhimanyu: 2014-01-04 13:08:13

what is the answer for 11 ??

Abhimanyu: 2014-01-04 12:51:31

can somebody explain why is the output of
1111111111
is
911111 as mentioned in comments and not
9111111 since there are 9 1s and 1 escape den 1 1s 1 escape and 1 for terminating .. plzz help

Himanshu: 2013-06-16 19:31:34

esay one but cost me about 1 hours.....


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