Jump to content
  • Advertisement
Sign in to follow this  
guyaton

fast comparison

This topic is 4823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a fast way to compare one string for subset of other strings? What I am trying to do is take a std::string and compare a subset of strings that could be contained witin that string for example: say the string is: some sample string for testing purposes and the comparing strings are: noun something is test blah for and the result would tell me that only "for" was repeated ( i dont' really care if i get the position in the origional string just that "for" was repeated (either by index or by returning the string). thanks, guyaton

Share this post


Link to post
Share on other sites
Advertisement
I'd break down the string into smaller pieces, separated by " " ... i.e words.


pseudo:

while (not at end of the string)
{
word = GatherLettersUntilSpaceIsFound();

CheckForMatch(word);
}


hmm... maybe that wasn't very helpful... :/

anyway, peace!

Share this post


Link to post
Share on other sites
Quote:
Original post by guyaton
yes, they are always whole words.
Great, that makes things a lot easier.
Then you could simply store all the words in a data structure with fast search times (such as a hash table or even std::map) and compare the input word-for-word.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Make a hash set of the string. Then you can quickly look-up if any word exists in the string. Constructing the hash set isn't that slow either. Anyway you'll get an O(N+M) algo where N is the amount of words in the test string and M the amount of compare words.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Anonymous Poster
Make a hash set of the string.
..of the words in the string, is what I meant.

Share this post


Link to post
Share on other sites
The most efficient way to do this in the general case would probably be to treat the list of keywords as a regular grammar, and build a FSA which flagged keywords as it found them. For more information on how to do this, read up on how regular expressions are compiled and processed.

Share this post


Link to post
Share on other sites
for clarification for myself, make a hash of the "comparing string" as i used in my example? havn't used a hash table in C++ but i know it is a member of the stl. how would i do it using c++ stl?

thanks again
~guyaton

Share this post


Link to post
Share on other sites
Depending on how long your strings are, you may be interested in the Boyer-Moore String matching algorithm.
[google]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!