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

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

Does it require suffix automaton??

DOT: 2018-08-15 12:16:32

Can anyone please provide pointers on the best solution for this? I used Trie, but its run time is much longer and my approach uses huge amount of memory as well.

Last edit: 2018-08-15 12:19:22
sabyasachi95: 2018-07-21 23:48:49

Any corner case for this problem?

mahmud2690: 2018-01-16 20:23:38

@pvsmpraveen: Actually, the container which you used in your TL solution, does not guarantee constant time access/insertion for any input. Also, you keep strings every time and it is definitely not O(1).

Last edit: 2018-01-16 20:28:20
pvsmpraveen: 2018-01-15 11:21:45

Although i got AC with my latest code,
I don't understand why i got TLE with O(N^2 + Q) using hashtable, it should pass given the constraints? or am i missing something?

julkas: 2017-10-08 14:48:50

@mahilewets. Don't think about KMP and other advanced algos. Try simple and short solution. My Python solution ~ 10 lines of code.

mahilewets: 2017-10-07 06:58:15

Maybe try something like more general KMP

amancaj: 2017-10-06 15:01:56

any hints on how to solve this?
the obvious straightforward string.find gives TLE... (tried in Python3.5 & C++)

wisfaq: 2017-10-02 16:26:15

The reason why I enter duplicate solutions is the instability of the SPOJ timing system.
You can see that my timings vary greatly for exact the same code.

Anyhow, you disqualified one or more of my solutions.
Disqualifying solutions by problem setters should be used only for getting rid of solutions by cheaters and the like. Now, thanks to you I have again acquired disqualified solutions while I did nothing wrong, except entering a solution more than once.
re: We were sorry about the inconvenience. However, we corrected our mistakes and we are now definitely sure that no further issues root from us.

Last edit: 2017-10-02 16:39:18
mahmud2690: 2017-10-02 15:36:02

@wisfaq: so sorry for inconvenience, we were having a debate on time limit and allowed languages. That is why we had to discard a few duplicate solutions, now everything is okay.

=(Francky)=> Please consider spoj_psolver not as beta_testers. Please publish a problem when fully ready, tested. Please do not delete comments of serious psolvers. Regards.
re: Okay, we will do our utmost in order to prevent such cases.

Last edit: 2017-10-02 15:43:27

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