Go语言字典树定义及实现
// trie 字典树实现
package Algorithm // 字典树节点
type TrieNode struct {
children map[interface{}]*TrieNode
isEnd bool
} // 构造字典树节点
func newTrieNode() *TrieNode {
return &TrieNode{children: make(map[interface{}]*TrieNode), isEnd: false}
} // 字典树
type Trie struct {
root *TrieNode
} // 构造字典树
func NewTrie() *Trie {
return &Trie{root: newTrieNode()}
} // 向字典树中插入一个单词
func (trie *Trie) Insert(word []interface{}) {
node := trie.root
for i := ; i < len(word); i++ {
_, ok := node.children[word[i]]
if !ok {
node.children[word[i]] = newTrieNode()
}
node = node.children[word[i]]
}
node.isEnd = true
} // 搜索字典树中是否存在指定单词
func (trie *Trie) Search(word []interface{}) bool {
node := trie.root
for i := ; i < len(word); i++ {
_, ok := node.children[word[i]]
if !ok {
return false
}
node = node.children[word[i]]
}
return node.isEnd
} // 判断字典树中是否有指定前缀的单词
func (trie *Trie) StartsWith(prefix []interface{}) bool {
node := trie.root
for i := ; i < len(prefix); i++ {
_, ok := node.children[prefix[i]]
if !ok {
return false
}
node = node.children[prefix[i]]
}
return true
}
github链接地址:https://github.com/gaopeng527/go_Algorithm/blob/master/trie.go
Go语言字典树定义及实现的更多相关文章
- [HNOI2004]L语言 字典树 记忆化搜索
[HNOI2004]L语言 字典树 记忆化搜索 给出\(n\)个字符串作为字典,询问\(m\)个字符串,求每个字符串最远能匹配(字典中的字符串)到的位置 容易想到使用字典树维护字典,然后又发现不能每步 ...
- 洛谷-P2292-L语言(字典树)
链接: https://www.luogu.org/problem/P2292 题意: 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是 ...
- hdu 1251:统计难题(字典树,经典题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- BZOJ 1212 L语言(DP+字典树)
求能被理解的最长前缀. 很显然的dp.令dp[i]=true,表示前缀i能理解.否则不能理解.那么dp[i+len]=dp[i]=true,当s[len]能匹配str[i,i+len]. 由于模式串长 ...
- Trie(字典树)解析及其在编程竞赛中的典型应用举例
摘要: 本文主要讲解了Trie的基本思想和原理,实现了几种常见的Trie构造方法,着重讲解Trie在编程竞赛中的一些典型应用. 什么是Trie? 如何构建一个Trie? Trie在编程竞赛中的典型应用 ...
- 初级字典树查找在 Emoji、关键字检索上的运用 Part-2
系列索引 Unicode 与 Emoji 字典树 TrieTree 与性能测试 生产实践 在有了 Unicode 和 Emoji 的知识准备后,本文进入编码环节. 我们知道 Emoji 是 Unico ...
- Tire树(字典树)
from:https://www.cnblogs.com/justinh/p/7716421.html Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,P ...
- 浅谈字典树Trie
\(\;\) 本文是作者学习<算法竞赛进阶指南>的所得,有些语言是摘自其中. \(\;\) 基础知识 定义 \(\;\) 字典树(Trie):是一种支持字符串查询的多叉树结构.其中的每个节 ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
随机推荐
- Centos6.5下通过shell脚本快速安装samba服务器
使用方法如下: 上传脚本到linux服务器授权
- byte数据常量池问题
[代码] public class BufferPoolDemo { public static void main(String[] args) { Integer i1=127; Integer ...
- js读取cookie,并利用encrypt和decrypt加密和解密方法
以下为支持encrypt和decrypt加密和解密方法 eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a ...
- Python 爬虫利器 Selenium 介绍
Python 爬虫利器 Selenium 介绍 转 https://mp.weixin.qq.com/s/YJGjZkUejEos_yJ1ukp5kw 前面几节,我们学习了用 requests 构造页 ...
- SpringMVC(十三) RequestMapping 使用servlet原生API作为参数
SpringMVC支持以下Servlet方法: HttpServletRequest HttpServletResponse HttpSession Writer Reader Locale Inpu ...
- Xamarin Essentials应用教程文件系统FileSystem
Xamarin Essentials应用教程文件系统FileSystem 文件系统用于管理设备内的各类文件.通过文件系统,应用程序可以创建永久文件和临时文件,也可以获取预先打包的文件,如预设数据库文件 ...
- webstorm过期最新激活方法
打开webstorm 注册时,在打开的License Activation窗口中选择“License server”,在输入框输入下面的网址:http://idea.iteblog.com/key.p ...
- c#取数据库数据 ---两种方法
通常有以下两种方式 SqlDataReader 和SqlDataAdapter|DataSet方式 SqlDataReader 方式使用方式如下: using System; using System ...
- c# 获取键盘的输入
c# 获取键盘的输入 Console 类公开了三个方法获取键盘的输入,分别是Read .Readkey.ReadLine Read方法: 每次只能读入一个字符,如果没有字符可以读,返回-1,Rea ...
- Node.js API快速入门
Node.js API 快速入门 一.事件EventEmitter const EventEmitter = require('events'); class MyEmitter extends Ev ...