STRMATCH - Match me if you can


After watching the movie "Catch me if you can" professor Mahammad became very confident about creating a new problem for his programmers. As some procedures in his research heavily depend on string matching, now, he wants to check his beginner programmers' skills in this topic as well. His task is very simple. Professor gives you a random string and several queries. For each of the query string, you have to count the number of its occurrences in the string provided by professor.

Input

First line of the input section contains two positive integers N and Q, which define the length of professor's string and the number of queries, respectively.

Second line contains professor's string having length N (N ≤ 3000).

The following Q lines contain a query string having nonzero length.

Output

For each of the queries, output the number of the desired count of the occurences.

Note: The sum of the length of query strings does not exceed 500000. And please, do consider the time limit, because the problem can be solved in both slow and fast languages.

Example

Input:

7 5
acababa
a
bb
caba
aba
karp

Output:

4
0
1
2
0


hide comments
karankaira: 2020-10-12 11:30:40

getting TLE with KMP ?? what should i Do??

binario: 2020-09-09 13:23:58

If you get TLE, use fast IO. And if using c ++ use '\ n' instead of endl.

sharath1999: 2020-08-04 16:37:18

Used Suffix automaton, probably the best string data structure

yaseenmollik: 2020-07-24 18:33:33

Nice problem. Solved using Rabin-Karp algorithm

trinhxhai: 2020-07-09 05:20:57

AC with SAM O(n + 5e5)
O(n + 5e5*log(26) ) will TLE :)) how ???

purplecs: 2020-02-15 14:36:39

easy......

narek: 2019-10-28 14:33:41

Getting TLE for O(n^2 + |Q|) using tries. It seems it works like that in the comments

Or are we supposed to implement a suffix array / suffix tree

Last edit: 2019-10-28 14:38:38
ilija_ir_13: 2019-08-12 11:55:05

1. Don't post any source code here.
2. Please be careful, leave short comments only. Don't spam here.
3. For more discussion (hints, ideas, solutions) please visit our forum.
4. Authors of the problems are allowed to delete the post and use html code here (e.g. to provide some useful links).

monty_007: 2019-01-01 19:11:09

Really nice test cases. Hashing just wouldnt work. In the end used trie with O(n^2 + |S|) complexity.

make9chaos: 2018-12-25 13:50:04

Does it require suffix automaton??


Added by:mahmud2690
Date:2017-09-29
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:Me, MYSELF & I