Trie树的插入,查前缀,查单词,删前缀和删单词。
这个Trie原先用C++就敲得很熟了,看了蓝桥杯的视频后学会把一个功能这样封装起来,以后用的时候就很爽可以直接调用了,所以就用Java写了;
public class Trie {
private final int SIZE = 26;
private final int HEAD = 'a';
private int cnt;
private int tail;
private Trie[] next = new Trie[SIZE];
void insert(String s) {
Trie now = this;
for (int i = 0; i < s.length(); i++) {
int id = s.charAt(i) - HEAD;
if (now.next[id] == null) {
now.next[id] = new Trie();
}
now = now.next[id];
now.cnt++;
}
now.tail++;
}
int queryPrefix(String s) {
Trie now = this;
for (int i = 0; i < s.length(); i++) {
int id = s.charAt(i) - HEAD;
if (now.next[id] == null) {
return 0;
}
now = now.next[id];
}
return now.cnt;
}
int queryWord(String s) {
Trie now = this;
for (int i = 0; i < s.length(); i++) {
int id = s.charAt(i) - HEAD;
if (now.next[id] == null) {
return 0;
}
now = now.next[id];
}
return now.tail;
}
void deletePrefix(String s) {
int sum = queryPrefix(s);
if (sum == 0) {
return;
}
Trie now = this;
for (int i = 0; i < s.length(); i++) {
int id = s.charAt(i) - HEAD;
now.next[id].cnt -= sum;
if (now.next[id].cnt == 0) {
now.next[id] = null;
return;
}
}
}
void deleteWord(String s) {
int sum = queryWord(s);
if (sum == 0) {
return;
}
Trie now = this;
for (int i = 0; i < s.length(); i++) {
int id = s.charAt(i) - HEAD;
now.next[id].cnt -= sum;
if (now.next[id].cnt == 0) {
now.next[id] = null;
return;
}
}
now.tail -= sum;
}
}
Trie树的插入,查前缀,查单词,删前缀和删单词。的更多相关文章
- trie树---(插入、删除、查询字符串)
HDU 5687 Problem Description 度熊手上有一本神奇的字典,你可以在它里面做如下三个操作: 1.insert : 往神奇字典中插入一个单词 2.delete: 在神奇字 ...
- 内存空间有限情况下的词频统计 Trie树 前缀树
数据结构与算法专题--第十二题 Trie树 https://mp.weixin.qq.com/s/nndr2AcECuUatXrxd3MgCg
- 双数组Trie树(DoubleArrayTrie)Java实现
http://www.hankcs.com/program/java/%E5%8F%8C%E6%95%B0%E7%BB%84trie%E6%A0%91doublearraytriejava%E5%AE ...
- 数据结构与算法—Trie树
Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...
- 字符串 --- KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组
涉及到字符串的问题,无外乎这样一些算法和数据结构:自动机 KMP算法 Extend-KMP 后缀树 后缀数组 trie树 trie图及其应用.当然这些都是比较高级的数据结构和算法,而这里面最常用和最熟 ...
- HDU 11488 Hyper Prefix Sets (字符串-Trie树)
H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...
- java实现的Trie树数据结构
近期在学习的时候,常常看到使用Trie树数据结构来解决这个问题.比方" 有一个1G大小的一个文件.里面每一行是一个词.词的大小不超过16字节,内存大小限制是1M. 返回频数最高的100个词. ...
- 6天通吃树结构—— 第五天 Trie树
原文:6天通吃树结构-- 第五天 Trie树 很有段时间没写此系列了,今天我们来说Trie树,Trie树的名字有很多,比如字典树,前缀树等等. 一:概念 下面我们有and,as,at,cn,com这些 ...
- 【动画】看动画轻松理解「Trie树」
Trie树 Trie这个名字取自“retrieval”,检索,因为Trie可以只用一个前缀便可以在一部字典中找到想要的单词. 虽然发音与「Tree」一致,但为了将这种 字典树 与 普通二叉树 以示区别 ...
随机推荐
- idea启动服务连接mysql后 Navicat连接mysql就报错2013-Lost connection toMySQL server at
我是使用navicat的windows端 连接centos下mysql服务器 第一次常规连接mysql正常,idea启动服务连接mysql后 Navicat连接mysql就报错2013-Lost co ...
- 65)STL中string的知识
1)代码展示: string是一个类,只不过封装了 char* 而且还封装了 很多的字符串操作函数 2)string类的初始化: string的构造函数 ² 默认构造函数: string(); ...
- CodeForces - 1243D. 0-1 MST(补图连通分量个数)
Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math notebooks: ...
- 2.Jenkins结合k8s完成Jenkins slave功能
1.构建镜像 下载基础镜像,这里使用openvz的包,下载地址为:https://wiki.openvz.org/Download/template/precreated,下载centos7的镜像 下 ...
- 你知道你对 JSON Web Token 的认识存在误解吗
1.前言 JSON Web Token (JWT) 其实目前已经广为软件开发者所熟知了,但是 JOSE (Javascript Object Signing and Encryption) 却鲜有人知 ...
- set theory
set theory Apart from classical logic, we assume the usual informal concept of sets. The reader (onl ...
- day54-mysql-库、表、数据操作
. 什么是数据库 存储数据的仓库 . 什么数据: 大家所知道的都是数据.比如:你同学的名字,年龄,性别等等 . 数据库概念 .数据库服务器 .数据库管理系统 重点 .库 .表 .记录 .数据 参考画图 ...
- 基础服务系列-Jupyter Install TensorFlow
TensorFlow is a deep learning framework that provides an easy interface to a variety of functionalit ...
- eclipse利用sql语句对Oracle数据库进行操作
对Oracle数据库执行操作的sql语句中表名和列名都需用英文双引号("")括起来. 注(\为转义符) 1.插入数据 sql = "insert into \" ...
- 887A. Div. 64#模特的数学技巧(字符串处理)
问题出处:http://codeforces.com/problemset/problem/887/A 问题大意:对于给出的一串二进制数,能否通过去掉一些数字,使之成为十进制下64的倍数 #inclu ...