Statement
A DNA sequence is represented by a string . Several genes are also known, represented by strings and ordered from most relevant to least relevant. The genes are numbered sequentially, starting from 1.
queries are made. Each delimits a segment of between indices and , inclusive. For each query, determine the index of the most relevant gene that occurs entirely as a substring of that segment.
Input
The first line contains (). The second line contains the number of genes (). Each of the following lines contains one gene, from most to least relevant. Every gene is nonempty. The genes are distinct, and the sum of their lengths is at most .
The next line contains (). Each of the following lines contains (), the inclusive query bounds.
All strings contain only the letters A, C, G, and T.
Output
For each query, print the index of the most relevant gene that occurs entirely within the corresponding segment. If none occurs, print .
Examples
Input
ACAGACA
3
ACA
G
CA
3
1 7
2 6
1 2
Output
1
2
-1
In the first query, the segment is the entire sequence ACAGACA; its most relevant gene is ACA (index 1). In the second, the segment is CAGAC; its most relevant gene is G (index 2). In the third, the segment is AC, which contains no gene.
Input
ACGT
2
ACGT
GT
2
1 3
2 4
Output
-1
2
Tutorial
We use Aho–Corasick to enumerate every occurrence of every gene in .
Bounding the number of matches
Lemma. The total number of gene occurrences in is , where is the sum of the gene lengths.
Consider all genes whose occurrences end at one fixed position of . Since the genes are distinct, two of them cannot have the same length: the substring with a given length and ending position is unique. If there are such genes, their lengths are at least . Therefore
Since this sum is , we have . Applying this bound at every text position proves the lemma.
Processing queries offline
Group each query by its right endpoint , then scan from left to right with the Aho–Corasick automaton.
Suppose position has just been processed. For every gene occurrence ending there, let be its starting position and its gene index. At position , store the minimum index of any occurrence seen so far that starts there.
All stored occurrences already end at or before . Consequently, the answer to query is simply the minimum stored gene index over starting positions . This condition guarantees that the entire occurrence is inside the query interval.
We need a data structure supporting:
update(p, g): replace the value at position with its minimum with ;suffix_min(l): find the minimum value at positions .
Square-root decomposition
Split the starting positions into blocks of size . Store both the individual value at every position and the minimum value of each block.
An update only decreases one individual value and its block minimum, so it takes . For a suffix query, inspect individual positions until reaching a block boundary, use the precomputed minimum for every complete block, and inspect the final partial block. This takes
Aho–Corasick construction costs . Match enumeration performs constant-time updates, and the queries cost . The final complexity is
time and memory.