views:

100

answers:

1

I have noticed that it has solutions for matching multiple words in a given text, such as below: http://stackoverflow.com/questions/1099985/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~

A: 
  1. Put the words you want to search for in a hashtable, with the words as keys and the values initialized to 0.
  2. 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.
  3. 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.

Amber