字典树(Trie树相关)

208. Implement Trie (Prefix Tree)

Implement a trie with insertsearch, and startsWith methods. (Medium)

Note:
You may assume that all inputs are consist of lowercase letters a-z.

分析:

字典树即前缀匹配树,在空间不是很影响的情况下一般采用如下数据结构存储Trie节点

 class TrieNode {
public:
// Initialize your data structure here.
bool isWord;
TrieNode* child[];
TrieNode(): isWord(false){
memset(child, NULL, sizeof(child));
}
};

所以插入、查找等操作比较直观,直接见程序。

代码:

 class TrieNode {
public:
// Initialize your data structure here.
bool isWord;
TrieNode* child[];
TrieNode(): isWord(false){
memset(child, NULL, sizeof(child));
}
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
TrieNode* r = root;
for (int i = ; i < word.size(); ++i) {
if (r -> child[word[i] - 'a'] == NULL) {
r -> child[word[i] - 'a'] = new TrieNode(); }
r = r -> child[word[i] - 'a'];
}
r -> isWord = true;
} // Returns if the word is in the trie.
bool search(string word) {
TrieNode* r = root;
for (int i = ; i < word.size(); ++i) {
if (r -> child[word[i] - 'a'] == NULL) {
return false; }
r = r -> child[word[i] - 'a'];
}
if (r -> isWord) {
return true;
}
return false;
} // Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode* r = root;
for (int i = ; i < prefix.size(); ++i) {
if (r -> child[prefix[i] - 'a'] == NULL) {
return false; }
r = r -> child[prefix[i] - 'a'];
}
return true;
} private:
TrieNode* root;
}; // Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");

211. Add and Search Word - Data structure design

Design a data structure that supports the following two operations:

void addWord(word)
bool search(word)

search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter. (Medium)

For example:

addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true

Note:
You may assume that all words are consist of lowercase letters a-z.

分析:

继续沿用字典树的思路,只不过加入了“.”通配符,所以在查找过程中加入DFS即可,用辅助函数helper

代码:

 class TrieNode {
public:
// Initialize your data structure here.
bool isWord;
TrieNode* child[];
TrieNode(): isWord(false){
memset(child, NULL, sizeof(child));
}
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
TrieNode* r = root;
for (int i = ; i < word.size(); ++i) {
if (r -> child[word[i] - 'a'] == NULL) {
r -> child[word[i] - 'a'] = new TrieNode(); }
r = r -> child[word[i] - 'a'];
}
r -> isWord = true;
} //helper function for search, use to solve "." problem
bool helper(string word, int pos, TrieNode* curRoot) {
if (pos == word.size() && curRoot -> isWord) {
return true;
}
if (word[pos] != '.') {
if (curRoot -> child[word[pos] - 'a'] == NULL) {
return false;
}
else {
return helper(word, pos + , curRoot -> child[word[pos] - 'a']);
}
}
else {
bool flag = false;
for (int i = ; i < ; ++i) {
if (curRoot -> child[i] != NULL && helper(word, pos + , curRoot -> child[i]) ) {
flag = true;
}
}
return flag;
}
return true;
}
// Returns if the word is in the trie.
bool search(string word) {
return helper(word, , root);
} private:
TrieNode* root;
}; class WordDictionary {
public:
Trie dic;
// Adds a word into the data structure.
void addWord(string word) {
dic.insert(word);
} // Returns if the word is in the data structure. A word could
// contain the dot character '.' to represent any one letter.
bool search(string word) {
return dic.search(word);
}
}; // Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
 

最新文章

  1. C# winform窗体设计-数据库连接
  2. 使用JdbcTemplate简化JDBC操作 实现数据库操作
  3. linux下find查找命令用法
  4. 【Node】package.json
  5. input 类型为number型时,maxlength不生效?
  6. 给ie浏览器添加推荐浏览器提示
  7. 简单介绍如何使用robotium进行自动化测试
  8. 学习Yii框架,有哪些比较好的网站
  9. 第一章 搭建一个通用的.net core项目框架
  10. 探索Windows命令行系列(1):导航目录
  11. Linux-1-用户管理
  12. 【做题】agc016d - XOR Replace——序列置换&amp;环
  13. android开发笔记:Handler、Looper、MessageQueen、Message的关系
  14. bzoj3884上帝与集合的正确用法
  15. 内网服务器通过Squid代理访问外网
  16. linux socket编程示例
  17. [翻译] 扩张卷积 (Dilated Convolution)
  18. URAL 1880 Psych Up&#39;s Eigenvalues
  19. python 可视化 词云图
  20. 周记7——ios中picker滑动穿透bug

热门文章

  1. 使用MySQL会话变量实现窗口函数
  2. Eureka自我保护机制、健康检查的作用、actuator模块监控
  3. HTML5中类jQuery选择器querySelector和querySelectorAll的使用
  4. 巧用tar命令
  5. js校验文本框只能输入数字(包括小数)
  6. Unknown command: crawl
  7. 【笔记】LR响应时间
  8. 建筑设计类软件整理ACDSee,PS,CAD,Ecotect,SketchUp,Phoenics,Revit,Rhino,
  9. Linux安装Desktop 和 vncserver
  10. WebSocket前后端实现