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
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...

artful_dodger: 2017-10-19 19:17:32

@morass ... My submission in CPP14 times out (test 15) but in clang CPP it passes with 3.74 sec. Any idea why this might happen ?

caohoangtung: 2017-09-20 15:43:46

My first problem using trie :) AC in 1 go

da_201501181: 2017-06-28 11:12:57

AC in one GO..!! Simple application of trie..!

vivace: 2017-05-11 11:26:58

@morass i got your point . I will definitely try to make my solution better .

morass: 2017-05-11 09:19:29

@vivace: Good day to you.

Firstly, it can be written more "static"

Secondly, the trie can be represented in other ways than you did (more sparse - for example linked-list like)

GL

~/Morass


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