这个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接口交互写死的

    vue接口 写死的 RoleOfUserOrgRef: function ({ commit }, param) { return new Promise((resolve) => { $axi ...

  2. vue-router HTML5 History 模式(转自官网)

    vue-router 默认 hash 模式 -- 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载. 如果不想要很丑的 hash,我们可以用路由的 his ...

  3. PAT Advanced 1050 String Subtraction (20) [Hash散列]

    题目 Given two strings S1 and S2, S = S1 – S2 is defined to be the remaining string afer taking all th ...

  4. tensorflow实现卷积层的几种方式

    #coding:utf-8 #第一种实现 tf.nn import tensorflow as tf import tensorflow.contrib.slim as slim tf.reset_d ...

  5. linux服务器开放防火墙和端口,以及查询状态

    自己搞一个自己网站时候,购买的阿里云服务器,发现部署项目访问不到,首先需要确认入站规则是否配置. 一.安全组列表添加 1.打开安全组列表 2.添加入站规则 3.添加安全组规则 二.通过防火墙,开启端口 ...

  6. JKFZ%你赛炸裂祭

    Md爆40了身败名裂 上来就刚T1是什么习惯?居然不看T2导致明明能刚出正解却止步40 , T3找到原题看懂题解后却不敢交+难码 , 最近怕不是做毒瘤%你赛多了总以为T1能刚到点分 md最近怕不是炸了 ...

  7. FullPage.js中文帮助文档API

    fullPage.js的方法: 1. moveSectionUp() 功能:向上滚动一页. 2. moveSectionDown() 功能:向下滚动一页. 3. moveTo(section, sli ...

  8. ruoyi HttpUtils

    package com.ruoyi.common.utils.http; import java.io.BufferedReader; import java.io.IOException; impo ...

  9. Flink(二) —— 部署与任务提交

    一.下载&启动 官网上下载安装包,执行下列命令即启动完成. ./bin/start-cluster.sh 效果图 Flink部署模式 Standalone模式 Yarn模式 k8s部署 二.配 ...

  10. Equal Cut

    Equal Cut 题目描述 Snuke has an integer sequence A of length N. He will make three cuts in A and divide ...