#P3494. Synchronizing Code Words
Synchronizing Code Words
Synchronizing Code Words
The Byteotian Institute of Telecommunication (BIT) uses prefix codes in which each character of the alphabet is represented by a sequence of bits called a code word. In a prefix code no code word is a prefix of another; that is, if a code word is w, then no other code word begins with w. Formally, if a code word for a letter is given by
\(w\),
then for any other code word \(w'\) we have
\[
w \not\prec w',
\]
where \(w \prec w'\) means that \(w\) is a prefix (i.e. a leading fragment) of \(w'\).
Sometimes, due to transmission errors, some of the leading bits of an encoded message may be lost. In such cases the wrong characters may be decoded at the beginning. However, it turns out that for some characters the following property holds: if all the bits of the code word for that character arrive intact, then all the characters succeeding it are decoded correctly.
Byteasar, an engineer at BIT, noticed that in a given prefix code some code words have this synchronizing property. For instance, consider the following prefix code for the alphabet consisting of 5 characters:
Character | Code Word |
---|---|
A | 00 |
B | 10 |
C | 11 |
D | 010 |
E | 011 |
In this example, if some leading bits are lost from the encoded message, the decoding may be wrong until a synchronizing code word is encountered. Byteasar found that when the code word for D or E arrives completely the decoding from that point on is correct, while the other letters (A, B, and C) do not have this property.
Your task is to write a program that, given a prefix code, finds all the synchronizing code words. In this easier version, assume that a code word is synchronizing if and only if its length is at least 3. (In a real setting the property is more complicated but for this problem you can use this criterion.)
inputFormat
The input begins with a line containing a single integer \(n\) (1 ≤ \(n\) ≤ 100) — the number of characters in the alphabet. Each of the next \(n\) lines contains a character (an uppercase letter) and its code word (a nonempty string consisting only of the digits 0 and 1) separated by a space. It is guaranteed that the set of code words forms a prefix code.
outputFormat
Output, in a single line, all the characters whose code word is synchronizing (according to the criterion that its length is at least 3), in the order in which they appear in the input, separated by a single space. If there is no synchronizing code word, output an empty line.
sample
5
A 00
B 10
C 11
D 010
E 011
D E