Which datatype and methods should I use?

前端 未结 1 1542
栀梦
栀梦 2021-01-26 02:36

I am trying to write a kind of simple search engine. I have a determined number of main subjects that are associated with specific keywords. The aim is to recognize the main sub

1条回答
  •  礼貌的吻别
    2021-01-26 02:52

    You're looking for Trie data structure, it is the recommended way of doing starts with search. Here is a blog post talking about it. You can find the source here.

    Here's how use the above implementation, code from the above article.

    //Create trie
    Trie < string > trie = new Trie < string > ();
    
    //Add some key-value pairs to the trie
    trie.Put("James", "112");
    trie.Put("Jake", "222");
    trie.Put("Fred", "326");
    
    //Search the trie
    trie.Matcher.NextMatch('J'); //Prefix thus far: "J"
    trie.Matcher.GetPrefixMatches(); //[112, 222]
    trie.Matcher.IsExactMatch(); //false
    trie.Matcher.NextMatch('a');
    trie.Matcher.NextMatch('m'); //Prefix thus far: "Jam"
    trie.Matcher.GetPrefixMatches(); //[112]
    trie.Matcher.NextMatch('e');
    trie.Matcher.NextMatch('s'); //Prefix thus far: "James"
    trie.Matcher.IsExactMatch(); //true
    trie.Matcher.GetExactMatch(); //112
    
    //Remove a string-value pair
    trie.Remove("James");
    

    0 讨论(0)
提交回复
热议问题