当前位置:网站首页>【英雄哥六月集训】第 23天: 字典树
【英雄哥六月集训】第 23天: 字典树
2022-07-27 10:01:00 【如果我是温帅帅】
系列文章
字典树
又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。
一、 连接词
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;
}
}
二、 最长单词
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;
}
}
总结
getOrDefault(Object key, V defaultValue)
意思就是当Map集合中有这个key时,就使用这个key对应的value值,如果没有就使用默认值defaultValue
边栏推荐
- Leetcode.814. binary tree pruning____ DFS
- 使用 Kmeans聚类实现颜色的分割
- Matlab-创建文字云
- RobotFramework+Eclispe环境安装篇
- Visual slam lecture notes (I): Lecture 1 + Lecture 2
- Live countdown 3 days sofachannel 29 P2P based file and image acceleration system Dragonfly
- vs2019社区版下载教程(详细)
- Pygame: alien invasion
- Food safety | is sugar free really sugar free? These truths need to be known
- 3D restoration paper: shape painting using 3D generative advantageous networks and recurrent revolutionary networks
猜你喜欢

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

线代003

Open3d library installation, CONDA common instructions, importing open3d times this error solving environment: failed with initial frozen solve Retrying w

Provincial Emergency Management Department: Guangzhou can strive to promote the experience of emergency safety education for children

RobotFramework+Eclispe环境安装篇

StyleGAN论文笔记+修改代码尝试3D点云生成

女粉想要找男朋友,竟是为了...

备战金九银十Android面试准备(含面试全流程,面试准备工作面试题和资料等)
![Introduction to regular expressions of shell, general matching, special characters: ^, $,., * Character range (brackets): [], special characters: \, matching mobile phone number](/img/31/ed0d8c1a5327059f2de7493bec1c6c.png)
Introduction to regular expressions of shell, general matching, special characters: ^, $,., * Character range (brackets): [], special characters: \, matching mobile phone number

Ant高级-task
随机推荐
视觉SLAM十四讲笔记(一):第一讲+第二讲
Matlab/Simulink求解微分方程样例分享
hdu5288(OO’s Sequence)
Decision tree principle and case application - Titanic survival prediction
LeetCode.814. 二叉树剪枝____DFS
Example of ICP registration for PCL
Based on LSM tree idea Net 6.0 C # write a kV database (case version)
Matlab的不同进制转换
hdu5289(Assignment)
DES/3DES/AES区别
Shell变量、系统预定义变量$HOME、$PWD、$SHELL、$USER、自定义变量、特殊变量$n、$#、$*、[email protected]、$?、env看所有的全局变量值、set看所有变量
Ant高级-task
Color segmentation using kmeans clustering
samba服务器
Concurrent Park and unpark description
Food safety | is sugar free really sugar free? These truths need to be known
Mathematical reasoning: five couples get together and shake hands when they meet
How does data analysis solve business problems? Here is a super detailed introduction
文件上传漏洞绕过方法
原生input标签的文件上传