// 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语言字典树定义及实现的更多相关文章

  1. [HNOI2004]L语言 字典树 记忆化搜索

    [HNOI2004]L语言 字典树 记忆化搜索 给出\(n\)个字符串作为字典,询问\(m\)个字符串,求每个字符串最远能匹配(字典中的字符串)到的位置 容易想到使用字典树维护字典,然后又发现不能每步 ...

  2. 洛谷-P2292-L语言(字典树)

    链接: https://www.luogu.org/problem/P2292 题意: 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是 ...

  3. hdu 1251:统计难题(字典树,经典题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  4. BZOJ 1212 L语言(DP+字典树)

    求能被理解的最长前缀. 很显然的dp.令dp[i]=true,表示前缀i能理解.否则不能理解.那么dp[i+len]=dp[i]=true,当s[len]能匹配str[i,i+len]. 由于模式串长 ...

  5. Trie(字典树)解析及其在编程竞赛中的典型应用举例

    摘要: 本文主要讲解了Trie的基本思想和原理,实现了几种常见的Trie构造方法,着重讲解Trie在编程竞赛中的一些典型应用. 什么是Trie? 如何构建一个Trie? Trie在编程竞赛中的典型应用 ...

  6. 初级字典树查找在 Emoji、关键字检索上的运用 Part-2

    系列索引 Unicode 与 Emoji 字典树 TrieTree 与性能测试 生产实践 在有了 Unicode 和 Emoji 的知识准备后,本文进入编码环节. 我们知道 Emoji 是 Unico ...

  7. Tire树(字典树)

    from:https://www.cnblogs.com/justinh/p/7716421.html Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,P ...

  8. 浅谈字典树Trie

    \(\;\) 本文是作者学习<算法竞赛进阶指南>的所得,有些语言是摘自其中. \(\;\) 基础知识 定义 \(\;\) 字典树(Trie):是一种支持字符串查询的多叉树结构.其中的每个节 ...

  9. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

随机推荐

  1. Codeforces 420D Cup Trick 平衡树

    Cup Trick 平衡树维护一下位置. #include<bits/stdc++.h> #include <bits/extc++.h> #define LL long lo ...

  2. GFS 安装使用

    准备环境: 1.OS: Centos:7.2x86_64 2.主机 server1: 192.168.30.41 wohaoshuai1 server2: 192.168.30.42 wohaoshu ...

  3. Python 高级面向对象

    一.字段 1.字段包括:普通字段和静态字段,他们在定义和使用中有所区别,而最本质的区别是内存中保存的位置不同. a.普通字段属于对象(实例变量) b.静态字段属于类(类变量) 二.属性 对于属性,有以 ...

  4. python 格式话-占位符

    格式化输出:name = qjage = 30job = itsalary = 6000例1:字符串拼接方法,不建议,因为会在内存中开辟多块内存空间. info = '''---------- inf ...

  5. window下用taskkill杀死进程

    TASKKILL [/S system [/U username [/P [password]]]] { [/FI filter] [/PID processid | /IM imagename] } ...

  6. P1052 过河 线性dp 路径压缩

    题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...

  7. 005 使用SpringMVC开发restful API三--处理创建请求

    一:主要任务 1.说明 @RequestBody 映射请求体到java方法的参数 日期类型参数的处理 @Valid注解 BindingResult验证请求参数的合法性并处理校验结果 二:@Reques ...

  8. 怎样将一个Long类型的数据转换成字节数组

    直接上代码: //先写进去 long n = 1000000L; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutpu ...

  9. gradle根据不同渠道设置不同的开屏启动页

    需求:根据不同渠道,app的开屏启动页不一样 思路:因为app的启动页是在清单文件配置的,而清单文件最后是要和main里面的清单文件合并的,所以每个渠道都要配一个清单文件,在里面设置 然后在Andro ...

  10. QT5版本添加icon图标步骤

    QT5版本添加icon图标方法收藏 方法1: step1: 把要显示的图标文件,比如为1.ico文件放到工程v的根目录下 step2: 修改当前项目XXX.pro文件,在文件末尾添加如下内容(注意=的 ...