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.
getting TLE with KMP ?? what should i Do??
If you get TLE, use fast IO. And if using c ++ use '\ n' instead of endl.
Used Suffix automaton, probably the best string data structure
Nice problem. Solved using Rabin-Karp algorithm
AC with SAM O(n + 5e5)
Getting TLE for O(n^2 + |Q|) using tries. It seems it works like that in the comments
1. Don't post any source code here.
Really nice test cases. Hashing just wouldnt work. In the end used trie with O(n^2 + |S|) complexity.
Does it require suffix automaton??