Two forms of a message
Time limit1sMemory limit1024 MB
Pack a brace-wrapped list of integers into a hexadecimal string by encoding zero runs and length prefixes, then print the result.
- Level
Medium4 of 10
- Topics
- String, Implementation, Simulation
- Solved
- No attempts yet
Statement
Juku takes part in a student satellite project and is responsible for controlling the satellite with messages sent from the ground. A message is wrapped in a start mark and an end mark, '{' and '}', and contains integers separated by spaces. Worried about slow data links, Juku decides to give messages a shorter form.
A number written as text takes one byte, or 8 bits. A number written in hexadecimal takes only 4 bits. Some of the 16 hexadecimal digits can also be used to pack a message. (Hexadecimal uses 09 and AF as digits, where 09 have values and AF have values . The value of a two-digit hexadecimal number is .)
Juku packs messages by these rules:
- The start mark is the hexadecimal digit
Eand the end mark is the hexadecimal digitF. - If a number in the input message has 3 to 15 zeros in a row, replace them with the hexadecimal digit
B, followed by the length of the zero run as a hexadecimal digit3F. If the run is longer than 15, writeB, then0, then the length as two hexadecimal digits10FF. - Each number in the input message is written starting with the hexadecimal digit
A. If the length of is at most 15, writeAfollowed by the length as a hexadecimal digit1F. If the length of is greater than 15, writeA, then0, then the length as two hexadecimal digits10FF. In both cases, the digits of follow the length. If has more than two zeros in a row, pack them first by rule 2, and the length is the length of the packed result, not the length of the original number. - If the packed message has an odd number of hexadecimal digits, append
Dat the end, afterF.
Write a program that reads the message from the input file and writes the packed result in text form to the output file.
Input
The input file has exactly one line containing one message. The message has at most 100 positive integers, each less than .
Output
The output file has one line with the packed message in text form, where each hexadecimal digit is one character. The output is written this way so that a person can read it. If the hexadecimal digits were packed into real bytes, the result would be hard to read.