前缀树,trie树
前缀树:
假设一个字符串数组,“abcd”,"bcd","gef" , 构建一颗树,字母是在路径上,节点上最基本的存储的信息包括:
以这个节点结尾的 字符串的数量;
经过这个节点的字符串的数量;
一个字符串类型的数组arr1,另一个字符串类型的数组arr2;
(1)arr2中有哪些字符,是arr1中出现的?请打印
(2)arr2中有哪些字符,是作为arr1中某个字符串前缀出现的?请 打印
(3)arr2中有哪些字符,是作为arr1中某个字符串前缀出现的?请打印 arr2中出现次数最大的前缀
package my_basic.class_7;
public class Code_00_TrieTree {
//前缀树
public static class TrieNode{
public int end;
public int path;
public TrieNode[] nexts;
public TrieNode() {
end = 0;
path = 0;
nexts = new TrieNode[26]; //之前没写 空指针
}
}
public static class Trie{
private TrieNode root;
public Trie() {
root = new TrieNode();
}
public void insert(String word) {
if (word == null) {
return;
}
char[] chs = word.toCharArray();
int index = 0;
TrieNode node = root;
for (int i = 0; i < chs.length; i++) {
index = chs[i] - 'a';
if (node.nexts[index] == null) {
node.nexts[index] = new TrieNode();
}
node = node.nexts[index];
node.path++;
}
node.end++;
}
public int search(String word) {
if (word == null) {
return 0;
}
char[] chs = word.toCharArray();
int index = 0;
TrieNode node = root;
for (int i = 0; i < chs.length; i++) {
index = chs[i] - 'a';
if (node.nexts[index] == null) {
return 0;
}
node = node.nexts[index];
}
return node.end;
}
public void delete(String word) {
if (word == null) {
return;
}
char[] chs = word.toCharArray();
int index = 0;
TrieNode node = root;
for (int i = 0; i < chs.length; i++) {
index = chs[i] - 'a';
if (--node.nexts[index].path == 0) {
node.nexts[index] = null; //后面的节点直接删去
return;
}
node = node.nexts[index];
}
node.end--;
}
public int prefixNumber(String word) {
if (word == null) {
return 0;
}
char[] chs = word.toCharArray();
int index = 1;
TrieNode node = root;
for (int i = 0; i < chs.length; i++) {
index = chs[i] - 'a';
if (node.nexts[index] == null) {
return 0;
}
node = node.nexts[index];
}
return node.path;
}
}
public static void main(String[] args) {
String[] arr1 = {"abc","bcd","def","bcf"};
String[] arr2 = {"b","a","bc"};
Trie trie = new Trie();
for (int i = 0; i < arr1.length; i++) {
trie.insert(arr1[i]);
}
//arr2中有哪些字符,是arr1中出现的
for (int i = 0; i < arr2.length; i++) {
if (trie.search(arr2[i]) != 0) {
System.out.print(arr2[i] + " ");
}
}
System.out.println(" ");
System.out.println("===================================");
//arr2中有哪些字符,是作为arr1中某个字符串前缀出现的?
for (int i = 0; i < arr2.length; i++) {
if (trie.prefixNumber(arr2[i]) != 0) {
System.out.print(arr2[i] + " ");
}
}
System.out.println(" ");
System.out.println("===================================");
String res = null;
int pre = 0;
for (int i = 0; i < arr2.length; i++) {
int prefixNum = trie.prefixNumber(arr2[i]);
if (prefixNum != 0) {
System.out.print(arr2[i] + " ");
if(prefixNum >= pre) {
res = arr2[i];
pre = prefixNum;
}
}
}
System.out.println();
System.out.println(res);
// Trie trie = new Trie();
// System.out.println(trie.search("zuo"));
// trie.insert("zuo");
// System.out.println(trie.search("zuo"));
// trie.delete("zuo");
// System.out.println(trie.search("zuo"));
// trie.insert("zuo");
// trie.insert("zuo");
// trie.delete("zuo");
// System.out.println(trie.search("zuo"));
// trie.delete("zuo");
// System.out.println(trie.search("zuo"));
// trie.insert("zuoa");
// trie.insert("zuoac");
// trie.insert("zuoab");
// trie.insert("zuoad");
// trie.delete("zuoa");
// System.out.println(trie.search("zuoa"));
// System.out.println(trie.prefixNumber("zuo"));
}
}
前缀树,trie树的更多相关文章
- Atitit 常见的树形结构 红黑树 二叉树 B树 B+树 Trie树 attilax理解与总结
Atitit 常见的树形结构 红黑树 二叉树 B树 B+树 Trie树 attilax理解与总结 1.1. 树形结构-- 一对多的关系1 1.2. 树的相关术语: 1 1.3. 常见的树形结构 ...
- 字典树(Trie树)的实现及应用
>>字典树的概念 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树.与二叉查找树不同,Trie树的 ...
- [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序
一. 题目 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 274040 Accepted: 48891 ...
- 洛谷$P4585\ [FJOI2015]$火星商店问题 线段树+$trie$树
正解:线段树+$trie$树 解题报告: 传送门$QwQ$ $umm$题目有点儿长我先写下题目大意趴$QwQ$,就说有$n$个初始均为空的集合和$m$次操作,每次操作为向某个集合内加入一个数$x$,或 ...
- luoguP6623 [省选联考 2020 A 卷] 树(trie树)
luoguP6623 [省选联考 2020 A 卷] 树(trie树) Luogu 题外话: ...想不出来啥好说的了. 我认识的人基本都切这道题了. 就我只会10分暴力. 我是傻逼. 题解时间 先不 ...
- [转载]字典树(trie树)、后缀树
(1)字典树(Trie树) Trie是个简单但实用的数据结构,通常用于实现字典查询.我们做即时响应用户输入的AJAX搜索框时,就是Trie开始.本质上,Trie是一颗存储多个字符串的树.相邻节点间的边 ...
- 【HDU - 5790 】Prefix(主席树+Trie树)
BUPT2017 wintertraining(15) #7C 题意 求[min((Z+L)%N,(Z+R)%N)+1,max((Z+L)%N,(Z+R)%N)+1]中不同前缀的个数,Z是上次询问的结 ...
- 字典树 trie树 学习
一字典树 字典树,又称单词查找树,Trie树,是一种树形结构,哈希表的一个变种 二.性质 根节点不包含字符,除根节点以外的每一个节点都只包含一个字符: 从根节点到某一节点,路径上经过的字符串连接起 ...
- 【字符串算法】字典树(Trie树)
什么是字典树 基本概念 字典树,又称为单词查找树或Tire树,是一种树形结构,它是一种哈希树的变种,用于存储字符串及其相关信息. 基本性质 1.根节点不包含字符,除根节点外的每一个子节点都包含一个字符 ...
- 字典树 Trie树
什么是Trie树? 形如 其中从根节点到红色节点的路径上的字母所连成的字符串即为一个Trie树上所存的字符串. 比如,这个trie树上有ab,abc,bd,dda这些字符串. 至于怎么构建和查找或添加 ...
随机推荐
- 修改mac host文件绑定域名
打开终端在终端terminal中输入 sudo vi /etc/hosts 上一步输入完成之后按enter回车键,如果当前用户账号有密码,则在按完之后会提示输入密码,此时输入当前账户密码后继续按ert ...
- 51nod1241(连续上升子序列)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1241 题意:中文题诶- 思路:通过观察我们不难发现就是找连续 ...
- vue+element级联选择器对接后台数据
1.后台接口返回的数据肯定要和级联选择器的数据一致,所以我专门弄个model存放返回的值,如下:/** * @Auther: GGDong * @Date: 2019/4/3 10:30 */@Get ...
- 洛谷P2169 正则表达式
题目背景 小\(Z\)童鞋一日意外的看到小\(X\)写了一个正则表达式的高级程序,这个正则表达式程序仅仅由字符"\(0\)","\(1\)","\(. ...
- easyui---tabs(选项卡)
配置好easyui环境 1.笔记: tabs(选项卡) class:class="easyui-tabs" //<div class="easyui-tabs&qu ...
- jdbc查询
import java.util.ArrayList; import java.util.List; import org.springframework.jdbc.core.BeanProperty ...
- 转 event 'utl_file I/O':
The ASH report shows tables and data files with wait event 'utl_file I/O': CHANGES No changes. CAUSE ...
- B. Batch Sort
http://codeforces.com/contest/724/problem/B 被坑了,一开始以为如果有一行已经是排好序了,然后有一行需要转换的次数 >= 2的话,那就直接no了. 因为 ...
- Webstorm 激活
注册时,在打开的License Activation窗口中选择“License server”,在输入框输入下面的网址: http://idea.iteblog.com/key.php 点击:Acti ...
- 关于编译错误ambiguous call of overridden pre R14 auto-imported BIF get/1
今天写代码用到了进程字典,出现了一个编译错误 根据相关提示改成了erlang:put erlang/get以后即编译通过