#P2292. [HNOI2004] L 语言

    ID: 1262 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2004各省省选湖南O2优化状态压缩,状压AC 自动机

[HNOI2004] L 语言

Description

Punctuation appeared later than writing, so earlier languages had no punctuation. Now you need to process an article without punctuation.

An article TT consists of lowercase letters. A word WW also consists of lowercase letters. A dictionary DD is a set of words. We say an article TT is understandable under a dictionary DD if TT can be split into several parts and each part is a word in DD.

For example, if the dictionary DD includes the words $\texttt{is}, \texttt{name}, \texttt{what}, \texttt{your}$, then the article whatisyourname is understandable under DD because it can be split into 4 words: what, is, your, name, and each word belongs to DD. The article whatisyouname is not understandable under DD, but it is understandable under D=D{you}D' = D \cup \{\texttt{you}\}. A prefix whatis of this article is also understandable under DD, and it is the longest prefix that can be understood under DD.

Given a dictionary DD, your program needs to determine whether several articles are understandable under DD, and output the position of the longest prefix that can be understood under DD.

Input Format

The first line contains two integers nn and mm, indicating that the dictionary DD has nn words and there are mm articles to process.

The next nn lines each contain a string ss, representing a word in the dictionary DD.

The next mm lines each contain a string tt, representing an article.

Output Format

For each article in the input, output one line with a single integer, the position of the longest prefix that can be understood under the dictionary DD.

4 3 
is
name
what
your
whatisyourname
whatisyouname
whaisyourname

14
6
0

Hint

Explanation for Sample 1

  • For the first query, the entire article whatisyourname can be understood.
  • For the second query, the prefix whatis can be understood.
  • For the third query, no prefix can be understood.

Constraints

  • For 80%80\% of the data, it is guaranteed that m20m \leq 20, t106|t| \leq 10^6.
  • For 100%100\% of the data, it is guaranteed that 1n201 \leq n \leq 20, 1m501 \leq m \leq 50, 1s201 \leq |s| \leq 20, 1t2×1061 \leq |t| \leq 2 \times 10^6, and both ss and tt contain only lowercase English letters.

Tips

  • Pay attention to the impact of input reading on program efficiency.
  • Note that the string lengths marked in Constraints are per-string lengths, not the sum of lengths.

Notes

  • The testdata is strengthened; the first 80%80\% is the original testdata.

Translated by ChatGPT 5