这个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树的插入,查前缀,查单词,删前缀和删单词。的更多相关文章

  1. trie树---(插入、删除、查询字符串)

    HDU   5687 Problem Description 度熊手上有一本神奇的字典,你可以在它里面做如下三个操作:  1.insert : 往神奇字典中插入一个单词  2.delete: 在神奇字 ...

  2. 内存空间有限情况下的词频统计 Trie树 前缀树

    数据结构与算法专题--第十二题 Trie树 https://mp.weixin.qq.com/s/nndr2AcECuUatXrxd3MgCg

  3. 双数组Trie树(DoubleArrayTrie)Java实现

    http://www.hankcs.com/program/java/%E5%8F%8C%E6%95%B0%E7%BB%84trie%E6%A0%91doublearraytriejava%E5%AE ...

  4. 数据结构与算法—Trie树

    Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...

  5. 字符串 --- KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组

    涉及到字符串的问题,无外乎这样一些算法和数据结构:自动机 KMP算法 Extend-KMP 后缀树 后缀数组 trie树 trie图及其应用.当然这些都是比较高级的数据结构和算法,而这里面最常用和最熟 ...

  6. 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 ...

  7. java实现的Trie树数据结构

    近期在学习的时候,常常看到使用Trie树数据结构来解决这个问题.比方" 有一个1G大小的一个文件.里面每一行是一个词.词的大小不超过16字节,内存大小限制是1M. 返回频数最高的100个词. ...

  8. 6天通吃树结构—— 第五天 Trie树

    原文:6天通吃树结构-- 第五天 Trie树 很有段时间没写此系列了,今天我们来说Trie树,Trie树的名字有很多,比如字典树,前缀树等等. 一:概念 下面我们有and,as,at,cn,com这些 ...

  9. 【动画】看动画轻松理解「Trie树」

    Trie树 Trie这个名字取自“retrieval”,检索,因为Trie可以只用一个前缀便可以在一部字典中找到想要的单词. 虽然发音与「Tree」一致,但为了将这种 字典树 与 普通二叉树 以示区别 ...

随机推荐

  1. Vue 集成环信 全局封装环信WebSDK 可直接使用

    2019-11-25更新 npm install --save easemob-websdk请直接使用官方安装方式即可.import WebIM from 'easemob-websdk' 以下是最开 ...

  2. h5-语义化标签的兼容性问题

    1.html代码 <header>头</header> <nav>导航栏</nav> <main> <article>左< ...

  3. TCP/IP通信过程

    一.参考网址 1.以太网帧格式.IP数据报格式.TCP段格式+UDP段格式 详解 2. 二.TCP的建立过程 1.例子: 192.168.22.66 telenet到192.168.22.74的tcp ...

  4. 吴裕雄--天生自然 JAVA开发学习:Scanner 类

    import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanne ...

  5. CF 1096D Easy Problem [动态规划]

    题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 有一长度为n的字符串,每一字符都有一个权值,要求现在从中取出若干个字符,使得字符串中没 ...

  6. Centos7.6环境中安装zabbix3.4

    官网链接:https://www.zabbix.com/documentation/3.4/zh/manual/installation/install_from_packages 部署环境 虚拟机服 ...

  7. elasticsearch min_hash 应用分析

    需求作相似文本查询 爬虫作页面去重,会用到simhash,第一个想到的是用simhash算法 但在现有数据集(elasticsearch集群)上用simhash,成本高,simhash值还好计算,不论 ...

  8. 爬虫笔记(十三)——lxml库的使用

    HTML示例代码: text = ''' <div> <ul> <li class="item-0"><a href="link ...

  9. Solving ordinary differential equations I(Nonstiff Problems),Exercise 1.2:A wrong solution

    (Newton 1671, “Problema II, Solutio particulare”). Solve the total differential equation $$3x^2-2ax+ ...

  10. Exception in thread "main" java.lang.AbstractMethodError

    参考https://stackoverflow.com/questions/15758151/class-conflict-when-starting-up-java-project-classmet ...