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

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

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

hide comments
2017-10-01 16:03:55 Pranay
kmp times out ?

re: Yes, of course. The intended solution is something faster than KMP.

Last edit: 2017-10-01 16:11:03
2017-10-01 13:43:19
@mahmud2690 Nice problem.
P.S. Time limit is weak. My FIFO C++ solution is ~ 0.4s.
re: yeah, we did not consider alluring features of C++ which you used in your solution.
P.S. It's all right. My greetings to sunny Baku. My dream city.
re: Thanks for nice words. My e-mail is tr0j4n034@gmail.com. We can discuss further by e-mail if you wish. :)

P.S. Thank you. I have some questions. I'll try to mail it.

Last edit: 2017-10-01 16:04:20
2017-09-29 11:08:55 [Rampage] Blue.Mary
Do the input strings contain lowercase English letters only?

(btw, the problem can be solved within the time limit even if N <= 500000.)

re: yes, input contains only lowercase english letters.
and thank you for extra feedback about N <= 500000 case.

Last edit: 2017-09-29 11:14:36
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.