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.
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.
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.
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
Any corner case for this problem?
@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
Although i got AC with my latest code,
@mahilewets. Don't think about KMP and other advanced algos. Try simple and short solution. My Python solution ~ 10 lines of code.
Maybe try something like more general KMP
any hints on how to solve this?
The reason why I enter duplicate solutions is the instability of the SPOJ timing system.
@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.
kmp times out ?