Under construction

Still being built. Things may change

DNA Decoding

Time limit2sMemory limit512 MB

Summary
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 dd can be decoded with a gene set GG if dd can be written as the concatenation of one or more genes, d=g1g2…gkd = g_1g_2\ldots g_k, where each gig_i is a gene from GG. The same gene may appear in the decoding several times.

The team needs a computer system that maintains a valid gene set GG and an array DD of DNA strands. As the remains are analyzed, the scientists add a new gene to GG or append a new DNA strand to DD. It is guaranteed that no gene in GG is a prefix of another gene at any moment.

After each operation, the scientists want to know which strands in DD can now be decoded with the current set GG. After the ii-th operation, the system must report kik_i, the number of strands in DD that became decodable for the first time after that operation, followed by the kik_i indices of those strands. Strands are numbered from 1 in the order they were appended to DD. The answer to each operation must be printed before the next operation is revealed.

Input

The first line contains the number of operations nn (1≤n≤100 0001 \le n \le 100\,000).

Each of the next nn lines starts with + if the operation adds a gene to GG, or with ? if it appends a DNA strand to the end of DD. A space follows, then a string xix_i of lowercase Latin letters. The string sis_i that is actually added is derived from xix_i as follows.

If i=1i = 1, then si=xis_i = x_i. Otherwise, let ki−1k_{i-1} be the number of strands that became decodable for the first time after the previous operation. Move the first character of xix_i to the end, ki−1k_{i-1} times. In other words, sis_i is xix_i cyclically shifted left by ki−1k_{i-1}.

All strings are non-empty, and the total length of the strings over all operations does not exceed 10610^6. It is guaranteed that no gene in GG is a prefix of another gene at any moment.

Output

Print nn lines. On line ii, print kik_i, followed by the kik_i numbers of the strands in DD that became decodable for the first time after the ii-th operation. The numbers may be printed in any order.

Hint

The first three strings s1s_1, s2s_2, s3s_3 match the input strings. Since k3=1k_3 = 1, the string s4s_4 for the fourth operation is dabdab shifted left by 1, which gives abdabd. So the fourth operation appends abdabd to DD. Finally, k4=0k_4 = 0, so s5=x5s_5 = x_5.

Examples1

  1. Example 1

    Input
    5
    ? abcabd
    + abc
    ? abcabc
    ? dabdab
    + abd
    
    Expected output
    0
    0
    1 2
    0
    2 1 3