[hihoCoder] Trie树

简介: This is a application of the Trie data structure, with minor extension. The critical part in this problem is to count all the words that have a partic...

This is a application of the Trie data structure, with minor extension. The critical part in this problem is to count all the words that have a particualr prefix and the problem has given nice hints to make this extension.

Two functions require to be implemented: add a word to the Trie and search the Trie for all words with a particular prefix.

The code is as follows.

If you are not familiar with Trie, you may refer to this solution first to get the basic idea of it.

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class TrieNode {
 6 public:
 7     int count;
 8     TrieNode* children[26];
 9     TrieNode() {
10         count = 0;
11         for (int i = 0; i < 26; i++)
12             children[i] = NULL;
13     }
14 };
15 
16 class Dictionary {
17 public:
18     Dictionary() {
19         root = new TrieNode();
20     }
21     
22     void insert(char* word) {
23         TrieNode* run = root;
24         for (int i = 0; word[i]; i++) {
25             if (!(run -> children[word[i] - 'a']))
26                 run -> children[word[i] - 'a'] = new TrieNode();
27             run = run -> children[word[i] - 'a'];
28             run -> count++;
29         }
30     }
31     
32     int search(char* prefix) {
33         TrieNode* run = root;
34         for (int i = 0; prefix[i]; i++) {
35             if (run)
36                 run = run -> children[prefix[i] - 'a'];
37             else break;
38         }
39         if (!run) return false;
40         return run -> count;
41     }
42     
43 private:
44     TrieNode* root;
45 };
46 
47 int main(void) {
48     int dictSize;
49     while (scanf("%d", &dictSize) != EOF) {
50         Dictionary dictionary;
51         char word[20];
52         for (int i = 0; i < dictSize; i++) {
53             scanf("%s", word);
54             dictionary.insert(word);
55         }
56         int querySize;
57         scanf("%d", &querySize);
58         char prefix[20];
59         for (int i = 0; i < querySize; i++) {
60             scanf("%s", prefix);
61             printf("%d\n", dictionary.search(prefix));
62         }
63     }
64     return 0;
65 }
目录
相关文章
AC自动机
AC自动机
59 0
|
6月前
|
算法
代码随想录算法训练营第五十五天 | LeetCode 583. 两个字符串的删除操作、72. 编辑距离、编辑距离总结
代码随想录算法训练营第五十五天 | LeetCode 583. 两个字符串的删除操作、72. 编辑距离、编辑距离总结
54 1
|
6月前
|
人工智能 算法 Java
每日一刷《剑指offer》字符串篇之编辑距离
每日一刷《剑指offer》字符串篇之编辑距离
65 0
每日一刷《剑指offer》字符串篇之编辑距离
|
算法 C++ 容器
【C++杂货铺】会杂耍的二叉搜索树——AVLTree
【C++杂货铺】会杂耍的二叉搜索树——AVLTree
31 0
|
算法 测试技术 Android开发
LeetCode 周赛上分之旅 # 36 KMP 字符串匹配殊途同归
学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越抽象,它能覆盖的问题域就越广,理解难度也更复杂。在这个专栏里,小彭与你分享每场 LeetCode 周赛的解题报告,一起体会上分之旅。
78 0
|
存储 算法 C++
【每日算法Day 84】面试必考题:Trie(字典树/前缀树)的实现
【每日算法Day 84】面试必考题:Trie(字典树/前缀树)的实现
|
SQL 算法 数据挖掘
【边学边敲边记】LeetCode005:最长公共前缀
【边学边敲边记】LeetCode005:最长公共前缀
【边学边敲边记】LeetCode005:最长公共前缀
|
SQL 数据挖掘 API
【边学边敲边记】LeetCode013:反转字符串中的单词 III
【边学边敲边记】LeetCode013:反转字符串中的单词 III
【边学边敲边记】LeetCode013:反转字符串中的单词 III
|
SQL 前端开发 小程序
【边学边敲边记】LeetCode012:反转字符串
【边学边敲边记】LeetCode012:反转字符串
118 0
【边学边敲边记】LeetCode012:反转字符串
|
机器学习/深度学习 SQL 算法
【边学边敲边记】LeetCode003:最长回文子串
【边学边敲边记】LeetCode003:最长回文子串
112 0
【边学边敲边记】LeetCode003:最长回文子串