DNA Decoding
Time limit2sMemory limit512 MB
Add genes to a prefix-free set or append DNA strands, then report which strands become fully decodable for the first time after each operation.
- Level
Hard9 of 10
- Topics
- Trie, String matching, Dynamic programming
- Solved
- No attempts yet
Statement
Scientists are excavating fossilized remains of ancient creatures on a planet in a neighboring star system. They want to understand how the DNA strands of these creatures were assembled from genes.
Each DNA strand is a sequence of nucleotides. Each nucleotide is written as a lowercase Latin letter, so a DNA strand is a string of lowercase Latin letters.
A gene is also a string of lowercase Latin letters. In any valid set of genes, no gene is a prefix of another gene.
A DNA strand can be decoded with a gene set if can be written as the concatenation of one or more genes, , where each is a gene from . The same gene may appear in the decoding several times.
The team needs a computer system that maintains a valid gene set and an array of DNA strands. As the remains are analyzed, the scientists add a new gene to or append a new DNA strand to . It is guaranteed that no gene in is a prefix of another gene at any moment.
After each operation, the scientists want to know which strands in can now be decoded with the current set . After the -th operation, the system must report , the number of strands in that became decodable for the first time after that operation, followed by the indices of those strands. Strands are numbered from 1 in the order they were appended to . The answer to each operation must be printed before the next operation is revealed.
Input
The first line contains the number of operations ().
Each of the next lines starts with + if the operation adds a gene to , or with ? if it appends a DNA strand to the end of . A space follows, then a string of lowercase Latin letters. The string that is actually added is derived from as follows.
If , then . Otherwise, let be the number of strands that became decodable for the first time after the previous operation. Move the first character of to the end, times. In other words, is cyclically shifted left by .
All strings are non-empty, and the total length of the strings over all operations does not exceed . It is guaranteed that no gene in is a prefix of another gene at any moment.
Output
Print lines. On line , print , followed by the numbers of the strands in that became decodable for the first time after the -th operation. The numbers may be printed in any order.
Hint
The first three strings , , match the input strings. Since , the string for the fourth operation is dabdab shifted left by 1, which gives abdabd. So the fourth operation appends abdabd to . Finally, , so .