特里树

特里树

实现 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<word.length();i++){            if(!node.containskey(word.charat(i))){                node.put(word.charat(i),new node());            }            node = node.get(word.charat(i));        }        node.setflag();    }    //will take tc : o(len) of the word    public boolean search(string word) {        node node = root;        for(int i =0;i<word.length();i++){            if(!node.containskey(word.charat(i))){                return false;            }            node = node.get(word.charat(i));        }        return node.getflag();    }    //will take tc : o(len) of the prefix    public boolean startswith(string prefix) {         node node = root;        for(int i =0;i<prefix.length();i++){            if(!node.containskey(prefix.charat(i))){                return false;            }            node = node.get(prefix.charat(i));        }        return true;    }}/** * your trie object will be instantiated and called as such: * trie obj = new trie(); * obj.insert(word); * boolean param_2 = obj.search(word); * boolean param_3 = obj.startswith(prefix); */

trie数据结构二

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

阿里云AI平台 阿里云AI平台

阿里云AI平台

阿里云AI平台 26 查看详情 阿里云AI平台

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<word.length();i++){            if(!node.containskey(word.charat(i))){                node.put(word.charat(i),new node());            }            node = node.get(word.charat(i));            node.incrementcountprefix();        }        node.incrementendwith();    }    public int countwordsequalto(string word) {        // write your code here.         node node = root;        for(int i=0;i<word.length();i++){            if(node.containskey(word.charat(i))){                node  = node.get(word.charat(i));            }            else return 0;        }        return node.getendwith();//this will tell how many strings are         //ending with given word    }    public int countwordsstartingwith(string word) {         node node = root;        for(int i=0;i<word.length();i++){            if(node.containskey(word.charat(i))){                node  = node.get(word.charat(i));            }            else return 0;        }        return  node.getcountprefix(); // it will tell how many strings are starting         //with given word;    }    public void erase(string word) {         node node = root;        for(int i =0;i<word.length();i++){            if(node.containskey(word.charat(i))){                node = node.get(word.charat(i));                node.decrementcountprefix();            }            else return;        }        node.deleteendwith();    }}

完整字符串

// 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<s.length();i++){          char c = s.charat(i);          if(!node.containskey(c)){              return false;          }          node = node.get(c);          flag = flag && node.isend(); // this will check if the substring is also a string from the list of strings          //if(flag == false) return false; // this line will also work here because if any substring is not present as a string in the trie , then           // this string s won't be a complete string, and we can return false here only      }      return flag;  }  public void insert(string s){    node node = root;    for(int i =0;i<s.length();i++){        char c = s.charat(i);        if(!node.containskey(c)){            node.put(c, new node());        }        node = node.get(c);    }    node.setend(); // setting end of the string as this is the           //end of the current string s   }}class solution {    static node root;  public static string completestring(int n, string[] a) {      trie trie = new trie();      //storing all the string in the trie data structure      for(string s : a) trie.insert(s);      //finding out the comeplete string among all the list of strings      string completestring = "";      for(string s : a){          if(trie.checkifprefixpresent(s)){              if(completestring.length() < s.length()){                  completestring = s;              }              //lexographical selection : a.compareto(b) =-1 if a < b lexographically               else if(completestring.length() == s.length() && s.compareto(completestring) < 0){                  completestring = s;              }          }      }      return completestring.equals("") ? "none":completestring;  }}

计算不同子串的个数

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< s.length();i++){            String str = "";            for (int j =i;j< s.length();j++){                str = str+s.charAt(j);                sol.insert(str);            }        }        return count+1;    }    public void insert(String s){        Node node = root;        for(int i =0;i< s.length();i++){            if(!node.containsKey(s.charAt(i))){                node.put(s.charAt(i),new Node());                count++;            }            node = node.get(s.charAt(i));        }        node.setFlag();    }}class Node {    Node node[] = new Node[26];    boolean flag;    public Node(){    }    public boolean containsKey(char c){        return node[c-'a']!=null;    }    public Node get(char c){        return node[c-'a'];    }    public void put(char c, Node n){        node[c-'a'] = n;    }    public void setFlag(){        this.flag = true;    }    public boolean getFlag(){        return this.flag;    }}

以上就是特里树的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/509021.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
抖音精选知识分享怎么做_抖音精选干货类视频创作指南
上一篇 2025年11月8日 23:49:55
防止sql注入有哪些PHP
下一篇 2025年11月8日 23:50:04

相关推荐

  • XML的DOM的DocumentFragment有什么用?

    documentfragment通过批量操作dom节点显著提升页面性能。它作为内存中的虚拟容器,允许开发者在不触发重绘和回流的情况下构建或修改节点结构,待所有操作完成后一次性插入文档。相较于逐个添加节点会引发多次渲染,使用documentfragment可减少浏览器的计算压力。其与普通元素节点不同之…

    2025年12月17日
    100
  • XPath的string()函数转换规则是什么?

    要提取特定元素的文本内容,可使用string()函数。对于给定html片段,提取div全部文本的方法是string(//div[@class=’content’]),结果包含所有后代文本节点;若只想提取p标签内文本而不包括a标签,则使用string-join(//div[@c…

    2025年12月17日
    100
  • XSLT的document()函数怎么加载外部XML?

    xslt的document()函数用于加载外部xml文件数据。1. 它通过xpath表达式调用,传入uri参数,返回外部xml文档的节点集;2. 典型用法包括整合多源数据、配置与查找表、模块化与重用以及处理大型xml文档;3. 路径解析支持绝对路径和相对路径,但需注意部署环境差异;4. 错误处理需检…

    2025年12月17日
    100
  • XML的DOM接口中NodeList怎么遍历?

    nodelist的遍历核心是利用length属性和索引访问节点,最稳妥的方式是使用传统for循环;1. nodelist分为“活的”和“死的”两种类型,“活的”会随dom变化实时更新,常见于getelementsbytagname、getelementsbyclassname和childnodes,…

    2025年12月17日
    200
  • XSD的substitutionGroup如何实现元素替换?

    xsd的substitutiongroup机制通过元素替代实现xml文档结构的多态性,使某个元素能被其“家族”中的其他成员替代,同时保持schema验证有效。具体步骤为:1. 定义头部元素(如vehicle),作为通用接口;2. 定义替代成员元素(如car、motorcycle),它们必须是全局元素…

    2025年12月17日
    100
  • XML的SAX解析器如何处理开始标签事件?

    sax解析器在开始标签事件中能提供uri、localname、qname及attributes四个关键信息。1. uri表示命名空间uri,用于区分不同命名空间下的同名标签;2. localname是不带命名空间前缀的本地标签名;3. qname是包含命名空间前缀的完整标签名;4. attribut…

    2025年12月17日
    000
  • XSLT的key()函数如何建立节点索引?

    xslt的key()函数通过预索引机制提升xml节点查找效率。1. 使用xsl:key声明索引,定义name(唯一名称)、match(匹配节点)、use(键值来源)属性;2. 在模板中调用key()函数,传入索引名和查找值,快速获取对应节点集。它解决了xpath//操作符在大型文档中重复遍历导致的性…

    2025年12月17日
    000
  • XQuery的validate表达式如何校验文档?

    xquery的validate表达式用于根据xml schema校验xml数据是否合规,其核心作用是确保数据结构和内容符合预期。它提供两种验证模式:1. strict模式要求数据完全符合schema定义,任何不匹配都会导致错误;2. lax模式仅验证schema中明确定义的部分,忽略未定义的内容。v…

    2025年12月17日
    100
  • RSS的item元素的guid有什么作用?

    guid在rss中的核心作用是为每个条目提供唯一标识以实现去重、更新追踪和稳定识别。具体包括:1.去重防漏:聚合器通过记录已处理的guid避免重复显示相同条目;2.内容更新追踪:当内容小幅修改但guid不变时,阅读器能识别为同一内容的更新而非新条目;3.作为永久链接:默认ispermalink=&#…

    2025年12月17日
    100
  • XPath的namespace轴在什么情况下使用?

    xpath的namespace轴关键在于处理带命名空间的xml/html文档,通过注册前缀与uri映射实现精准定位。1. 命名空间用于避免元素冲突,如book:title与cd:title属不同空间;2. xpath中直接使用前缀会失败,因需通过namespace context明确前缀对应uri;…

    2025年12月17日
    000
  • XSLT的apply-templates选择节点有哪些方式?

    xslt中apply-templates选择节点的方式主要有两种:1.通过select属性指定xpath表达式精准选择节点;2.不指定select属性时默认处理当前上下文的所有子节点。此外,结合mode属性可实现对相同节点的不同处理逻辑。使用select属性时,xpath表达式可以是相对路径、绝对路…

    2025年12月17日
    100
  • XSLT的mode属性在模板中起什么作用?

    xslt中的mode属性通过为模板提供“模式”概念,使同一xml节点在不同模式下可被不同模板处理。1. 定义模板时,在xsl:template上使用mode属性,如mode=”summary-view”或mode=”detail-view”,以区分不同…

    2025年12月17日
    000
  • XML的DOM的Attr接口有哪些属性?

    xml dom中的attr接口暴露了name、value、specified和ownerelement四个核心属性。name是只读字符串,表示属性名称;value是可读写字符串,用于获取或设置属性值;specified是布尔值,指示属性是否在文档中明确指定;ownerelement指向拥有该attr…

    2025年12月17日
    000
  • XLink的show属性有哪些可选值?

    xlink的show属性用于定义链接资源的展示方式,主要有五个值:new、replace、embed、other和none。new表示在新窗口打开;replace表示替换当前内容;embed表示将资源嵌入当前文档;other由应用程序自定义行为;none则不预设任何显示行为。相比html的targe…

    2025年12月17日
    000
  • XSL-FO的block-container如何定位内容?

    block-container在xsl-fo中用于创建独立布局上下文以实现高级定位和局部排版控制。1. 它为内部元素提供新的坐标系,支持绝对定位,允许子元素相对于容器进行left、top等属性的精确定位;2. block-container可设定width、height、边距等属性,与主文档流分离,…

    2025年12月17日
    000
  • XSD的restriction元素如何限制简单类型?

    xsd中restriction元素用于对简单类型进行约束,通过刻面限制值域。常用刻面包括:1.length、minlength、maxlength限制长度;2.pattern使用正则定义格式;3.enumeration限定可选值;4.mininclusive/maxinclusive等定义数值范围;…

    2025年12月17日
    000
  • XSLT的number元素如何格式化序号?

    xslt的number元素通过format、level、count等核心属性实现灵活的序号控制。1. format定义输出格式,如1、a、a、i、i及混合格式;2. level指定计数级别,包括single(默认)、multiple(多级编号)和any(全局连续计数);3. count设定要计数的节…

    2025年12月17日
    000
  • XSD的key和keyref如何定义数据关系?

    xsd中key和keyref机制用于定义xml文档内部数据的唯一性和引用完整性,其核心在于通过唯一键(key)和引用键(keyref)确保数据一致性。1. key用于定义唯一标识符,由selector指定目标元素集,field指定构成唯一值的属性或子元素,确保所选范围内该值全局唯一;2. keyre…

    2025年12月17日
    000
  • SOAP消息的Envelope元素有什么作用?

    soap消息的envelope元素是整个消息的根元素,它定义了消息的结构、协议版本和扩展性。1.envelope必须包含body元素,header为可选;2.通过xmlns:soap属性指定soap版本,如soap 1.1或soap 1.2;3.header用于传递元数据,如安全信息、路由信息等,并…

    2025年12月17日
    000
  • XML如何定义别名机制?

    xml没有官方的“别名机制”,但通过命名空间、实体引用和schema的ref属性实现了类似功能。1.命名空间通过前缀绑定uri,避免元素名冲突,如soap:envelope中的soap是uri的别名;2.实体引用通过定义通用或参数实体实现内容复用,如用&copyright;代替固定文本;3.…

    2025年12月17日
    100

发表回复

登录后才能评论
关注微信