Each of n binary words has at most one unreadable bit; decide whether the ?s can be filled so no word is a prefix of another.
Hard8TrieGreedyStringDFSNo attempts yetTime limit2sMemory limit2048 MBBen has just learned about binary prefix codes. A binary code is a set of n distinct nonempty code words si, each made of 0s and 1s. A code is a prefix code if for every i=j, si is not a prefix of sj and sj is not a prefix of si. A word x is a prefix of a word w if there is a word y, possibly empty, with xy=w. For example, x=11 is a prefix of w=110, and x=0100 is a prefix of w=0100.
Ben found a paper with n lines of binary code on it. The paper is old, so some of the characters cannot be read. Each word contains at most one unreadable character.
Decide whether these n lines can represent a binary prefix code. That is, decide whether Ben can replace every unreadable character with 0 or 1 so that the whole set becomes a prefix code.
The first line contains the number of code words n (1≤n≤5×105).
Each of the next n lines contains one code word record. A record is nonempty and consists of the characters 0, 1 and ?, where ? stands for an unreadable character. Every record contains at most one ?.
The total length of the records does not exceed 5×105.
Print YES on the first line if every ? can be replaced by 0 or 1 so that the words form a prefix code, and NO otherwise. Do not print the completed code words.