ADAINDEX - Ada and Indexing


Ada the Ladybug has many things to do and almost no time. She wants to to save time while searching for something so she have decided to make a search engine.

She has many words in her TODO list. It costs her precious time to find out, whether a word is in it so she seeks your help. You will get list and some queries. You will be asked, to find out how many words in TODO list have a word as prefix.

Input

The first line contains N, Q: the number words in TODO list and number of queries.

N lines follow, with words (of TODO list) consisting of lowercase letters. The sum of their lengths won't be greater than 106

Q lines follow, with words (queries) consisting of lowercase letters. The sum of their lengths won't be greater than 106

Output

For each query print the number of words in TODO list which have actual word as prefix.

Example Input

12 6
bulldog
dog
dogged
doggedly
doggerel
dogma
dogmatic
dogmatism
dogs
catastroph
catastroph
doctor
cat
dog
dogg
do
doctrinography
dogge

Example Output

2
8
3
9
0
3

hide comments
vaishcr7: 2019-07-11 20:35:08

@morass . Can you tell me the issue with y submission 24137100, the same way as you helped bhumit.

Last edit: 2019-07-26 08:59:39
raftar2097: 2019-04-20 21:30:14

@cardinalx i tried using hash but got tle

Last edit: 2019-04-20 21:30:28
ameernsr: 2019-04-09 12:45:33

right!
@cardinalx

cardinalx: 2019-04-09 12:44:24

can solve it with hashing

Last edit: 2019-04-09 12:46:11
ameernsr: 2019-04-09 12:07:19

badihi

DOT: 2018-09-13 17:42:38

Easy question. AC in one go!

julkas: 2018-08-04 16:33:56

Not only Trie. Try other data structure or algorithm.

Last edit: 2018-08-05 10:21:23
singlasahil221: 2018-05-17 06:57:16

AC in one go. Easy implementation of Trie.

flyingduchman_: 2017-12-12 13:42:42

You don't need bool isEndOfWord as there is no delete operation required, instead use a counter variable for each node to smoothly handle duplicates. While inserting, increment the counter variable, thus duplicates will be counted.

avik26091998: 2017-11-22 20:09:02

Good problem for data structure implementation...


Added by:Morass
Date:2016-09-06
Time limit:6s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 GOSU