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 ...
随机推荐
- Codeforces 746F Music in Car
Music in Car 用两个Set维护一下尺取的过程. #include<bits/stdc++.h> #define LL long long #define fi first #d ...
- docker inspect命令
docker inspect -f {{.NetworkSettings.Networks.crawling_pro.NetworkID}} crawling_internationalmacro_p ...
- Centos7创建CA和申请证书
转载:http://rackie386.blog.51cto.com/11279229/1947999 Centos7.3创建CA和申请证书 openssl 的配置文件:/etc/pki/tls/op ...
- Unknown lifecycle phase "mvn"
Unknown lifecycle phase "mvn" maven执行命令错误 : 执行输入命令即可,不需要添加 mvn 此处不需要写mvn,而是执行写compile就行,否 ...
- IDEA创建SpringBoot项目
创建SpringBoot有三种方式: 方式一:(常用方式)
- hdu-2043解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2043 题意解析:目的是判断一串密码是否安全,条件是满足1.大写字母:A,B,C...Z; 2.小写 ...
- Android Studio项目生成Jar包
步骤: 1)在module的gradle文件中,将apply plugin:'com.android.application'改为apply plugin:'com.android.library' ...
- 1402 后缀数组 (hash+二分)
描述 后缀数组 (SA) 是一种重要的数据结构,通常使用倍增或者DC3算法实现,这超出了我们的讨论范围.在本题中,我们希望使用快排.Hash与二分实现一个简单的 O(n log^2n ) 的后缀数组 ...
- python 数据结构之二叉树
二叉树关键在构建和遍历,python实现相对简单,我们在实现需要用到类,分别设置爱左右子树,根节点,然后从根进行遍历,进行判断,若为空进行树的构建,非空则返回到列表中即可,我在进行遍历时产生了一个错误 ...
- CCF-炉石传说
#include<cstdio> typedef struct { int attack,health; } cus; cus info[][]; ]; ; void summon(); ...