首页 > 其他 > 详细

字典树——add && find

时间:2019-03-21 12:21:55      阅读:135      评论:0      收藏:0      [点我收藏+]
 1 struct node{
 2     map<char, struct node*> children;
 3     string word;
 4     node(){
 5         word = "";
 6     }
 7 };
 8 class Trie{
 9     public:
10         struct node *root = NULL;
11         Trie(){
12             root = new struct node();
13         }
14         void addWord(string word){
15             struct node *cur = root;
16             for(int i = 0; i < word.length(); i++){
17                 if (cur->children.find(word[i]) == cur->children.end()) {
18                     cur->children[word[i]] = new struct node();
19                 }
20                 cur = cur->children[word[i]];
21             }
22             cur->word = word;
23         }
24         bool findWord(string word){
25             struct node *cur = root;
26             for(int i = 0; i < word.length(); i++){
27                 if(cur->children.find(word[i]) == cur->children.end()){
28                     return false;
29                 }
30                 cur = cur->children[word[i]];
31             }
32             return cur->word != "";
33         }
34 };

 

字典树——add && find

原文:https://www.cnblogs.com/liqiniuniu/p/10570106.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!