”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 特里树

特里树

发布于2024-07-31
浏览:771

Trie

实现 Trie 数据结构

Trie数据结构的Strider讲解

class Node{
    Node [] node = new Node[26];
    boolean flag;
    public Node(){

    }
    public boolean containsKey(char c){
        return node[c-'a']!=null;
    }
    public void put(char c, Node n){
        node[c-'a']  = n;
    }
    public Node get(char c){
        return node[c-'a'];
    }
    public void setFlag() {
        this.flag = true;
    }
    public boolean getFlag(){
        return this.flag;
    }
}

class Trie {
    Node root;
    public Trie() {
        root = new Node();
    }
    //will take tc : O(len) of the word
    public void insert(String word) {
        Node node  = root;
        for(int i =0;i



Trie数据结构二

奋斗者的解释,以便更好地理解

import java.util.* ;
import java.io.*; 

class Node {
    Node node[] = new Node[26];
    int endWith = 0;// will keep track of no. of words ending with this word
    int countPrefix=0;// will keep track of no. of words starting with this word
    public Node(){

    }
    public boolean containsKey(char c){
        return node[c-'a']!=null;
    }
    public void put(char c, Node n){
        node[c-'a'] = n;
    }
    public Node get(char c){
        return node[c-'a'];
    }
    public void incrementCountPrefix() {
          this.countPrefix;
    }
    public void decrementCountPrefix(){
        --this.countPrefix;
    }
    public void incrementEndWith(){
          this.endWith;
    }
    public void deleteEndWith(){
        --this.endWith;
    }
    public int getCountPrefix(){
        return this.countPrefix;
    }
    public int getEndWith(){
        return this.endWith;
    }


}
public class Trie {
    Node root;
    public Trie() {
        // Write your code here.
        root = new Node();
    }

    public void insert(String word) {
        Node node = root;
        for(int i =0;i



完整字符串

// tc : O(n*l)

import java.util.* ;
import java.io.*; 

class Node{
    Node[] node = new Node[26];
    boolean flag;
    public Node(){}
    public void put(char c , Node n){
        node[c-'a'] = n;
    }
    public boolean containsKey(char c){
        return node[c-'a']!=null;
    }
    public Node get(char c){
        return node[c-'a'];
    }
    public void setEnd(){
        this.flag = true;
    }
    public boolean isEnd(){
        return this.flag;
    }
}

class Trie{
    Node root;
    public Trie(){
        root = new Node();
    }
    public boolean checkIfPrefixPresent(String s){
      Node node = root;
      boolean flag= true;
      for(int i = 0;i



计算不同子串的个数

Tc:在
中插入不同的唯一子字符串的 O(n^2) Trie数据结构

import java.util.ArrayList;

public class Solution 
{
    Node root;
    static int count;
    public Solution(){
        root = new Node();
    }

    public static int countDistinctSubstrings(String s) 
    {
        count = 0;
        //    Write your code here.
        Solution sol = new Solution();
        for(int i =0;i




          

            
  

            
        
版本声明 本文转载于:https://dev.to/prashantrmishra/trie-e56?1如有侵犯,请联系[email protected]删除
最新教程 更多>
  • C ++ 17中引入的关键语言和库功能是什么?
    C ++ 17中引入的关键语言和库功能是什么?
    C 17 的新功能在C 17的功能开发之后,已经引入了几个新的语言功能和库添加: [2 class templatesRepresentation of values of any type with template <auto>Lambda Enhancements: 简介Co...
    编程 发布于2025-02-06
  • PHP阵列键值异常:了解07和08的好奇情况
    PHP阵列键值异常:了解07和08的好奇情况
    PHP数组键值问题,使用07&08 在给定数月的数组中,键值07和08呈现令人困惑的行为时,就会出现一个不寻常的问题。运行print_r($月份)返回意外结果:键“ 07”丢失,而键“ 08”分配给了9月的值。此问题源于PHP对领先零的解释。当一个数字带有0(例如07或08)的前缀时,PHP将...
    编程 发布于2025-02-06
  • 如何修复\“常规错误:2006 MySQL Server在插入数据时已经消失\”?
    如何修复\“常规错误:2006 MySQL Server在插入数据时已经消失\”?
    插入记录时如何解决“一般错误:2006 MySQL 服务器已消失”介绍:将数据插入 MySQL 数据库有时会导致错误“一般错误:2006 MySQL 服务器已消失”。当与服务器的连接丢失时会出现此错误,通常是由于 MySQL 配置中的两个变量之一所致。解决方案:解决此错误的关键是调整wait_tim...
    编程 发布于2025-02-06
  • 如何有效地逆转Java 8流?
    如何有效地逆转Java 8流?
    反向java 8 streams 特定的intstream reversal 可以创建一个自定义方法以相反的顺序映射值范围。例如,如果我们的intstream范围从-5到0,则将其倒流将导致流0到-5。可以使用以下代码来实现这一点:此方法避免进行拳击和排序,从而产生了更有效的解决方案。 @cust...
    编程 发布于2025-02-06
  • 版本5.6.5之前,使用current_timestamp与时间戳列的current_timestamp与时间戳列有什么限制?
    版本5.6.5之前,使用current_timestamp与时间戳列的current_timestamp与时间戳列有什么限制?
    在默认值中使用current_timestamp或mysql版本中的current_timestamp或在5.6.5 这种限制源于遗产实现的关注,这些限制需要为Current_timestamp功能提供特定的实现。消息和相关问题 `Productid` int(10)unsigned not ...
    编程 发布于2025-02-06
  • 如何使用不同数量列的联合数据库表?
    如何使用不同数量列的联合数据库表?
    合并列数不同的表 当尝试合并列数不同的数据库表时,可能会遇到挑战。一种直接的方法是在列数较少的表中,为缺失的列追加空值。 例如,考虑两个表,表 A 和表 B,其中表 A 的列数多于表 B。为了合并这些表,同时处理表 B 中缺失的列,请按照以下步骤操作: 确定表 B 中缺失的列,并将它们添加到表的末...
    编程 发布于2025-02-06
  • 如何防止在HTML元素上拖放?
    如何防止在HTML元素上拖放?
    如何在Web设计中禁用拖放在网络设计中,对于控制用户与某些HTML的交互是至关重要的元素。一个普遍的挑战是在不可能期望的情况下防止浏览器启动拖放操作。暂时禁用拖放功能,您可以使用以下方法: 通过在这些事件处理程序中返回false,您可以有效防止浏览器启动拖放操作。这样可以确保用户的预期操作(例如调...
    编程 发布于2025-02-06
  • 大批
    大批
    [2 数组是对象,因此它们在JS中也具有方法。 切片(开始):在新数组中提取部分数组,而无需突变原始数组。 令arr = ['a','b','c','d','e']; // USECASE:提取直到索引作...
    编程 发布于2025-02-06
  • 如何从PHP服务器发送文件?
    如何从PHP服务器发送文件?
    将文件发送到user
    编程 发布于2025-02-06
  • 可以在纯CS中将多个粘性元素彼此堆叠在一起吗?
    可以在纯CS中将多个粘性元素彼此堆叠在一起吗?
    &lt;/main&gt; &lt;section&gt; ,但无法使其正常工作,如您所见。任何洞察力都将不胜感激! display:grid; { position:sticky; top:1em; z-index:1 1 ; { { { pos...
    编程 发布于2025-02-06
  • 如何限制动态大小的父元素中元素的滚动范围?
    如何限制动态大小的父元素中元素的滚动范围?
    在交互式界面中实现垂直滚动元素的CSS高度限制 考虑一个布局,其中我们具有与可滚动的映射div一起移动的subollable map div用户的垂直滚动,同时保持其与固定侧边栏的对齐方式。但是,地图的滚动无限期扩展,超过了视口的高度,阻止用户访问页面页脚。 可以限制地图的滚动,我们可以利用CSS...
    编程 发布于2025-02-06
  • 如何使用固定的标头和可滚动车身创建Bootstrap 3表?
    如何使用固定的标头和可滚动车身创建Bootstrap 3表?
    用粘性定位一个简单但有效的解决方案涉及使用CSS粘性定位。这种方法涉及分配位置:粘性;顶部:0;到表标头(TH)元素,以确保它们保持固定在表的顶部。这是实现的分解:在上面的代码中,我们使用class tablefixhead创建一个容器,其中包括溢出:auto;使表可滚动和100px的高度定义可...
    编程 发布于2025-02-06
  • 如何在整个HTML文档中设计特定元素类型的第一个实例?
    如何在整个HTML文档中设计特定元素类型的第一个实例?
    [2单独使用CSS,整个HTML文档可能是一个挑战。 the:第一型伪级仅限于与其父元素中类型的第一个元素匹配。 以下CSS将使用添加的类样式的第一个段落: }
    编程 发布于2025-02-06
  • 如何在SQL中的单个字段中有效搜索多个值?
    如何在SQL中的单个字段中有效搜索多个值?
    [2进入其组成部分,并单独搜索每个部分。当处理以空格分离的搜索词时,这一点特别有用。在SQL中实现此目标,人们可能会考虑使用与通配符的类似运算符在每个搜索词之前和之后匹配任何字符。但是,这种方法不是很有效,因为它需要多个类似的子句,并且可能导致性能问题。相反,一个更有效的解决方案是使用在运算符中。在...
    编程 发布于2025-02-06
  • 如何使用$ _GET在PHP中检索多个选择框值?
    如何使用$ _GET在PHP中检索多个选择框值?
    在php array。解决方案:作为选项数组,您需要将方括号添加到Select元素的名称中。这是您的代码的修改版本:”多个...> 此更改使PHP能够将所选选项解释为数组。然后,您可以使用以下代码在PHP脚本中访问它: Header(“ content-type:text/plain”); fo...
    编程 发布于2025-02-06

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3