The Code
Time limit1sMemory limit128 MB
Given a prefix code entered via button presses, find the code words that resynchronize decoding after any loss of leading bits.
- Level
Hard9 of 10
- Topics
- Trie, String, Graph, Implementation
- Solved
- No attempts yet
Problem
A prefix code assigns to every character of an alphabet a distinct binary string called its code word. The code words satisfy two rules:
- No code word is a prefix of another. For example, if
010010is the code word of a letter, then none of0,01,010,0100,01001, nor any string that begins with010010, is the code word of another letter. - If a bit string is a prefix of some code word but is not itself a complete code word, then each of and (that is, with a
0or a1appended) is again a prefix of some code word or a complete code word. For example, if0100is a prefix of some code word, then01000and01001are each a prefix of some code word or a complete code word.
Equivalently, the code words are the leaves of a full binary tree in which every internal node has exactly two children.
Here is an example prefix code over the alphabet {A, B, C, D, E}:
A message is encoded by concatenating the code words of its characters in order. For example, BACAEBABAE encodes to 1000110001110001000011.
If some leading bits of an encoded message are lost, the message may be decoded incorrectly, or not decoded at all. For example, dropping the first five bits above leaves 10001110001000011, which decodes as BACBABAE: the last five letters (BABAE) are correct, but the first three (BAC) are not. Notice that every letter after the first E is decoded correctly. In fact, once every bit of the code word of E has been read intact, all following characters are decoded correctly, no matter which leading bits were lost. The code word of D shares this property, but those of A, B, and C do not.
A code word with this property is called synchronizing: if the decoder reads it in full and correctly, then, regardless of any lost leading bits, every character after it is decoded correctly. Your task is to find all synchronizing code words of a given prefix code.
The code words are presented on a device with four buttons:
0- append the bit0to the display.1- append the bit1to the display.B- backspace: delete the last bit shown on the display.X- beep: signals that the display currently shows a complete code word.
The display starts empty. Each code word is entered by pressing buttons until it appears on the display, and then pressing X. Code words are numbered in the order in which their X presses occur. After the last code word, the display is cleared with as many B presses as needed.
Input
The first line contains an integer (): the number of button presses. The second line is a string of characters over 0, 1, B, and X describing the presses in order. Each X completes one code word, and code words are numbered starting from . The total length of all code words does not exceed .
Output
On the first line print the number of synchronizing code words. Then print the numbers of the synchronizing code words in increasing order, one per line. If there are no synchronizing code words, print a single line containing .
Example
In the example input the buttons enter five code words in order: 11, 10, 00, 011, and 010. Among them, 011 (code word 4) and 010 (code word 5) are synchronizing, so the answer lists 4 and 5.