主要是记录一下这个数据结构。

比如这个trie树,包含三个单词:sea,sells,she。

代码:

 class Trie {
bool isWord;
vector<Trie*> children;
public:
/** Initialize your data structure here. */
Trie() {
isWord=false;
children.assign(,nullptr);
} /** Inserts a word into the trie. */
void insert(string word) {
if(word.empty()){return;}
auto cur=this;
for(int i=;i<word.size();++i){
if(cur->children[word[i]-'a']==nullptr){
cur->children[word[i]-'a']=new Trie();
}
cur=cur->children[word[i]-'a'];
}
cur->isWord=true;
} /** Returns if the word is in the trie. */
bool search(string word) {
auto cur=this;
for(int i=;i<word.size();++i){
if(cur->children[word[i]-'a']==nullptr){
return false;
}
cur=cur->children[word[i]-'a'];
}
return cur->isWord;
} /** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
auto cur=this;
for(int i=;i<prefix.size();++i){
if(cur->children[prefix[i]-'a']==nullptr){
return false;
}
cur=cur->children[prefix[i]-'a'];
}
return true;
}
}; /**
* Your Trie object will be instantiated and called as such:
* Trie* obj = new Trie();
* obj->insert(word);
* bool param_2 = obj->search(word);
* bool param_3 = obj->startsWith(prefix);
*/

最新文章

  1. Codeforces Round #352 (Div. 2)
  2. [转]架构蓝图--软件架构 &quot;4+1&quot; 视图模型
  3. 背水一战 Windows 10 (5) - UI: 标题栏
  4. Java-基础练习1
  5. 7月15日学习之BOM
  6. objective-c宏定义
  7. Linux 下实时查看日志
  8. C#读写Shapefile
  9. 邓_ ThinkPhp框架
  10. 学习总结javascript和ajax,php,和css
  11. 秒懂C#通过Emit动态生成代码
  12. Python 判断文件/目录是否存在
  13. jenkins使用jacoco插件检测代码覆盖率(八)
  14. metasploit framework(十一):获取漏洞信息
  15. Kafka学习之路 (三)Kafka的高可用
  16. 安装了XAMPP,PHP怎么显示中文
  17. zendframework配置篇
  18. Quercus-基于 Java 的 PHP 框架
  19. Web服务器对比介绍
  20. 微信小程序挑一挑辅助

热门文章

  1. CGI、FastCGI和php-fpm的概念和区别和运行原理
  2. python正式学习第二天
  3. Java各种类
  4. web端常见测试
  5. py 二级习题(重新输出文本-----每行一句话)
  6. Linux内核提权漏洞(CVE-2019-13272)
  7. IO流学习之字节流(二)
  8. Python标准库之hashlib模块与hmac模块
  9. 通过设置iis在局域网中访问网页
  10. 关闭 APIPA