#P7641. [BalticOI 2006] RLE COMPRESSION (Day 2)
[BalticOI 2006] RLE COMPRESSION (Day 2)
Description
RLE is a simple compression algorithm used to compress sequences that contain consecutive repetitions of the same character. By compressing a given sequence, we obtain its encoding. The idea is to use a counter to represent repetitions of a given character (e.g. ), indicating how many times it repeats. That is, we represent it using a triple consisting of a repetition marker, the repeated character, and an integer that represents the number of repetitions. For example, can be encoded as (where is the repetition marker).
We need some way to represent letters, the repetition marker, and the counter. Let the alphabet consist of characters, represented by integers from the set . The encoding of a character sequence over is also a character sequence over . At any moment, the repetition marker is represented by a character from , denoted by . Initially, is , but it may change during the encoding.
The encoding is interpreted as follows:
- Any character in the encoding, except for the repetition marker, represents itself.
- If the repetition marker occurs in the encoding, then the next two characters have special meanings:
- If is followed by , it represents repetitions of .
- Otherwise, if is followed by (where ), then becomes the repetition marker starting from that point.
- Otherwise, if is followed by (where and ), it represents repetitions of .
Using the scheme above, we can encode any character sequence over . For example, for , the sequence can be encoded as . The first character in the encoding represents the plain character . The next encodes . Then represents , represents , and switches the repetition marker to . After that, represents itself, and finally encodes .
A sequence can be encoded in multiple ways, and the length of the encoding may vary. Given an already encoded sequence, your task is to find an encoding with the minimum number of characters.
Write a program that:
- Reads the size of the alphabet and the encoding of the sequence.
- Finds the shortest encoding of the sequence.
- Outputs the result.
Input Format
The first line contains an integer , the size of the alphabet. The second line contains an integer , the length of the encoding. The last line contains integers from , separated by single spaces, representing the encoding of the sequence.
Output Format
The first line should contain an integer , the minimum number of characters in an encoding that represents the given sequence. The last line of output should contain integers from the set , separated by single spaces, giving an encoding of the sequence. If there are several shortest encodings, the program may output any one of them.
4
20
1 0 0 1 0 2 3 0 3 2 0 1 0 0 3 0 2 1 0 1
19
1 0 1 0 0 0 1 2 3 1 3 2 0 3 0 2 1 0 1
14
15
10 10 10 0 10 0 10 10 13 10 10 13 10 10 13
9
0 10 13 0 10 13 0 10 10
Hint
Constraints
For of the testdata, , .
Notes
The problem is from Baltic Olympiad in Informatics 2006, Day 2: RLE. Translated and整理 (pinyin: zhengli) by
Translated by ChatGPT 5
京公网安备 11011102002149号