当前位置:网站首页>[brother hero June training] day 23: dictionary tree
[brother hero June training] day 23: dictionary tree
2022-07-27 10:21:00 【If I were Wen Shuai】
Series articles
【 Brother hero training in June 】 The first 01 God : Array
【 Brother hero training in June 】 The first 02 God : character string
【 Brother hero training in June 】 The first 03 God : Sort
【 Brother hero training in June 】 The first 04 God : greedy
【 Brother hero training in June 】 The first 05 God : Double pointer
【 Brother hero training in June 】 The first 06 God : The sliding window
【 Brother hero training in June 】 The first 07 God : Hash
【 Brother hero training in June 】 The first 08 God : The prefix and
【 Brother hero training in June 】 The first 09 God : Two points search
【 Brother hero training in June 】 The first 10 God : An operation
【 Brother hero training in June 】 The first 11 God : matrix
【 Brother hero training in June 】 The first 12 God : Linked list
【 Brother hero training in June 】 The first 13 God : Double linked list
【 Brother hero training in June 】 The first 14 God : Stack
【 Brother hero training in June 】 The first 15 God : Binary tree
【 Brother hero training in June 】 The first 16 God : queue
【 Brother hero training in June 】 The first 17 God : Breadth first search
【 Brother hero training in June 】 The first 18 God : Trees
【 Brother hero training in June 】 The first 19 God : Binary tree
【 Brother hero training in June 】 The first 20 God : Binary search tree
【 Brother hero training in June 】 The first 21 God : Pile up ( Priority queue )
【 Brother hero training in June 】 The first 22 God : Ordered set
【 Brother hero training in June 】 The first 23 God : Dictionary tree
List of articles
Dictionary tree
Also known as word search tree ,Trie Trees , It's a tree structure , It's a variation of the hash tree . The typical application is for statistics , Sort and save a lot of strings ( But it's not just about strings ), So it is often used in text word frequency statistics by search engine system . Its advantages are : Use the public prefix of the string to reduce query time , Minimize unnecessary string comparisons , Query efficiency is higher than hash tree .
One 、 Conjunctions
class Solution {
class Trie{
Trie[] children;
boolean isEnd;
public Trie(){
children = new Trie[26];
isEnd = false;
}
}
Trie trie= new Trie();
public List<String> findAllConcatenatedWordsInADict(String[] words) {
List<String> ans = new ArrayList<String>();
Arrays.sort(words,(a,b)->a.length()-b.length());
for(int i=0; i< words.length; i++){
String word = words[i];
if(word.length()==0){
continue;
}
boolean[] visited = new boolean[word.length()];
if(dfs(word,0,visited)){
ans.add(word);
} else {
insert(word);
}
}
return ans;
}
public boolean dfs(String word,int start,boolean[] visited){
if(word.length() == start){
return true;
}
if(visited[start]){
return false;
}
visited[start] = true;
Trie node = trie;
for( int i=start; i<word.length(); i++){
char ch = word.charAt(i);
int index = ch - 'a';
node = node.children[index];
if(node == null){
return false;
}
if(node.isEnd){
if(dfs(word,i+1,visited)){
return true;
}
}
}
return false;
}
public void insert(String word){
Trie node = trie;
for(int i = 0;i<word.length();i++){
char ch=word.charAt(i);
int index = ch -'a';
if(node.children[index]==null){
node.children[index]= new Trie();
}
node = node.children[index];
}
node.isEnd=true;
}
}
Two 、 The longest word
Interview questions 17.15. The longest word
class Solution {
class Trie{
Trie[] children;
boolean isEnd;
public Trie(){
children = new Trie[26];
isEnd = false;
}
}
Trie trie= new Trie();
public boolean dfs(String word,int start){
Trie node = trie;
if(word.length() == start){
return true;
}
for(int i=start;i<word.length();i++){
int c=word.charAt(i)-'a';
node=node.children[c];
if(node == null) return false;
if(node.isEnd){
if(dfs(word,i+1)){
return true;
}
}
}
return false;
}
public void insert(String word){
Trie node = trie;
for(int i = 0;i<word.length();i++){
char ch=word.charAt(i);
int index = ch -'a';
if(node.children[index]==null){
node.children[index]= new Trie();
}
node = node.children[index];
}
node.isEnd=true;
}
public String longestWord(String[] words) {
String ret="";
Arrays.sort(words,(o1,o2)->o1.length()-o2.length());
for(String word:words){
if(word.length()==0) continue;
if(dfs(word,0)){
if(ret.length()<word.length()){
ret=word;
} else if(ret.length()==word.length()){
if(ret.compareTo(word)>0){
ret = word;
}
}
}
else{
insert(word);
}
}
return ret;
}
}
summary
getOrDefault(Object key, V defaultValue)
It means when Map There's this in the collection key when , Just use this key Corresponding value value , If not, use the default value defaultValue
边栏推荐
- hugo学习笔记
- StyleGAN论文笔记+修改代码尝试3D点云生成
- Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号
- Matlab-绘制日期和持续时间图
- Leetcode.814. binary tree pruning____ DFS
- mount.nfs: access denied by server while mounting解决
- Solve oracle-ora-01122 01110 01210
- Snowflake vs. databricks who is better? The latest war report in 2022
- Ubuntu and MySQL quick start tutorial
- Summary of binary tree exercises
猜你喜欢

怎样关闭电脑开机自启动的应用

Reason for pilot importerror: cannot import name 'pilot_ Version 'from' PIL ', how to install pilot < 7.0.0

Leetcode.814. binary tree pruning____ DFS

NFT system development - Tutorial

超赞的卡尔曼滤波详解文章

Xiandai 004

Based on LSM tree idea Net 6.0 C # write a kV database (case version)

warning package.json: No license field报错

Anchor Free检测器:CenterNet

3D face reconstruction and dense alignment with position map progression network
随机推荐
Food safety | are you still eating fermented rice noodles? Be careful these foods are poisonous!
Live countdown 3 days sofachannel 29 P2P based file and image acceleration system Dragonfly
NVIDIA geforce experience login error: the verifier failed to load. Please check your browser settings, such as the advertisement interceptor (solution)
warning package.json: No license field报错
Learn typescript (1)
WGAN、WGAN-GP、BigGAN
【英雄哥六月集训】第 23天: 字典树
如何创建一个带诊断工具的.NET镜像
数据库性能系列之子查询
Wind10 configure ADB command
VS2019+CUDA11.1新建项目里没有CUDA选项
Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号
Matlab-基于短时神经网络的声音分类
程序的翻译和执行,从编辑、预处理、编译、汇编、链接到执行
Leetcode.814. binary tree pruning____ DFS
Pytorch installation (very detailed)
Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量
Text processing tool in shell, cut [option parameter] filename Description: the default separator is the built-in variable of tab, awk [option parameter] '/pattern1/{action1}filename and awk
Ant高级-task
Open3d library installation, CONDA common instructions, importing open3d times this error solving environment: failed with initial frozen solve Retrying w