QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 128 MB Total points: 110

#16172. L Language

Statistics

Punctuation marks appeared later than written language, so ancient languages did not have punctuation. You are to process a piece of text that lacks punctuation.

A piece of text $T$ consists of several lowercase letters. A word $W$ also consists of several lowercase letters. A dictionary $D$ is a collection of several words. We say that a piece of text $T$ is "understandable" under a dictionary $D$ if $T$ can be partitioned into several parts, where each part is a word in $D$.

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

Given a dictionary $D$, your program needs to determine whether several pieces of text are understandable under $D$, and output the position of the longest prefix that is understandable under $D$.

Input

The first line of the input file contains two positive integers $n$ and $m$, representing that there are $n$ words in the dictionary $D$ and $m$ pieces of text to be processed.

The following $n$ lines each describe a word, and the subsequent $m$ lines each describe a piece of text.

Constraints: $1 \le n, m \le 20$, the length of each word does not exceed 10, and the length of each piece of text does not exceed 1M.

Output

For each piece of text provided, you need to output the position of the longest prefix of that text that is understandable under dictionary $D$.

Examples

Input 1

4 3
is
name
what
your
whatisyourname
whatisyouname
whaisyourname

Output 1

14
6
0

Note

The example output corresponds to the following: 14 (the entire text 'whatisyourname' is understandable) 6 (the prefix 'whatis' is understandable) 0 (no prefix is understandable)

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.