I have noticed that it has solutions for matching multiple words in a given text, such as below: Algorithm for multiple word matching in text
If I want to开发者_运维百科 know exactly the number of appearances of each matched word in the text, my solution is like this:
step 1: using ac-algorithm to obtain the maching words;
step 2: count the number of each word obtained in step 1
is there a faster way?
Thx~
- Put the words you want to search for in a hashtable, with the words as keys and the values initialized to 0.
- Iterate over the words of the text, each time checking to see if the word is a key in the hashtable, if so, then increment the value for that key.
- Iterate over the hashtable finding the values which are non-zero, the keys for these are your matched words, the values are the counts.
Runs in O(N+M) where N is the number of words you're searching for and M is the number of words you're searching through.
精彩评论