当前位置:网站首页>[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
边栏推荐
- sql注入
- SE(Squeeze and Excitation)模块的理解以及代码实现
- Oracle 11g manual memory management
- 怎样关闭电脑开机自启动的应用
- Matlab-基于短时神经网络的声音分类
- How to create a.Net image with diagnostic tools
- Configuration of pytorch deep learning environment based on cuda10.0
- 语音识别的一些开源项目整理
- pillow的原因ImportError: cannot import name ‘PILLOW_VERSION‘ from ‘PIL‘,如何安装pillow<7.0.0
- pytorch中对BatchNorm2d()函数的理解
猜你喜欢
![Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号](/img/31/ed0d8c1a5327059f2de7493bec1c6c.png)
Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号

Failure of CUDA installation nsight visual studio edition failed

关于ETL的两种架构(ETL架构和ELT架构)

There is no CUDA option in vs2019+cuda11.1 new project

Dcgan paper improvements + simplified code

Basic statement of database operation

Ant高级-path和fileset

Shell综合应用案例,归档文件、发送消息

WGAN、WGAN-GP、BigGAN

Oracle调整数据文件大小杂谈
随机推荐
VS2019+CUDA11.1新建项目里没有CUDA选项
Huawei switch dual uplink networking smart Link Configuration Guide
Switch port mirroring Configuration Guide
Cannot start after installing MySQL 5.7.27 in CentOS 7? (Language bash)
Open3d library installation, CONDA common instructions, importing open3d times this error solving environment: failed with initial frozen solve Retrying w
WGAN、WGAN-GP、BigGAN
Visual slam lecture notes (I): Lecture 1 + Lecture 2
Shell function, system function, basename [string / pathname] [suffix] can be understood as taking the file name in the path, dirname file absolute path, and user-defined function
Data visualization
samba服务器
Solve oracle-ora-01122 01110 01210
3D修复论文:Shape Inpainting using 3D Generative Adversarial Network and Recurrent Convolutional Networks
Sub query of database performance series
Matlab-创建 MATLAB的logo
gyp ERR! configure error. gyp ERR! stack Error: gyp failed with exit code: 1
视觉SLAM十四讲笔记(一):第一讲+第二讲
Shell operator, $((expression)) "or" $[expression], expr method, condition judgment, test condition, [condition], comparison between two integers, judgment according to file permission, judgment accor
PCL的ICP配准示例
Preparation for Android interview (including the whole process of interview, interview preparation, interview questions and materials, etc.)
NVIDIA geforce experience login error: the verifier failed to load. Please check your browser settings, such as the advertisement interceptor (solution)