我应该使用哪种数据类型和方法?

我正在尝试编写一种简单的搜索引擎。 我有确定数量的与特定关键字相关的主要主题。 目的是从输入的部分关键字识别主要主题。 我正在考虑使用: Dictionary<string, List> 。 我将不得不在这个字典中搜索并找到例如以3个字符的string开头的所有关键字以及它们相关联的主要主题。

我的解决方案是最好的吗? 如何有效地查看这些数据,而无需手动检查每个List ,逐string

如果我不清楚,请告诉我。

您正在寻找Trie数据结构 ,这是从搜索开始的推荐方式。 这是一篇关于它的博客文章 。 你可以在这里找到来源 。

以下是如何使用上面的实现,代码来自上面的文章。

 //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");