单词查找树Trie树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。

python函数引用传递
列表、字典、集合:按引用传入函数

def change(mylist):mylist[0:2]=[2,3]mylist = [11,22,33,44]
print()
change(mylist)
print(mylist)

[2, 3, 33, 44]

#node:[计数,子树]
def refreshTrieNode(prevChar,nextChar,nowNode,isEndChar):if  prevChar in nowNode.keys() and (not nowNode[prevChar][1] is None):if nextChar in nowNode[prevChar][1].keys():if isEndChar:nowNode[prevChar][1][nextChar][0]+=1newNode=nowNode[prevChar][1]else: if isEndChar:newNodeData=[1,None]else:newNodeData=[0,None]          nowNode[prevChar][1][nextChar]=newNodeDatanewNode=nowNode         else:if isEndChar:newNode={nextChar:[1,None]}else:newNode={nextChar:[0,None]}           nowNode[prevChar][1]=newNode  return newNode,nextChar def searchTrieNode(prevChar,nextChar,nowNode,isEndChar):if  (not nowNode is None) and prevChar in nowNode.keys():newNode=nowNode[prevChar][1]if not newNode is None:if nextChar in newNode.keys():if isEndChar: if newNode[nextChar][0]>0:return newNode,nextChar,False,True,newNode[nextChar][0]#,,continue,findedelse:       return newNode,nextChar,True,False,None#,,continue,findedreturn None,None,False,False,None#,,continue,finded
#code:刘兴,https://blog.csdn.net/AI_LX
def test():words=["ab","ab","ad","adcd","add"]print(words)rootNode={"ROOT":[0,None]}for word in words:node=rootNodeprevChar="ROOT"for i in range(len(word)):if i==len(word)-1:isEndChar=Trueelse:isEndChar=Falsechar=word[i]node,prevChar=refreshTrieNode(prevChar,char,node,isEndChar)print("===")print(rootNode)print("---")words=["aa","ab","addd","abc","ad","adcd"]for word in words:node=rootNodeprevChar="ROOT"for i in range(len(word)):if i==len(word)-1:isEndChar=Trueelse:isEndChar=Falsechar=word[i]node,prevChar,isContinue,isFind,wc=searchTrieNode(prevChar,char,node,isEndChar)if not isContinue:if isFind:print(f"找到{word},共有{wc}个。")else:print(f"找不到{word}")break test()
['ab', 'ab', 'ad', 'adcd', 'add']
===
{'ROOT': [0, {'a': [0, {'b': [2, None], 'd': [1, {'c': [0, {'d': [1, None]}], 'd': [1, None]}]}]}]}
---
找不到aa
找到ab,共有2个。
找不到addd
找不到abc
找到ad,共有1个。
找到adcd,共有1个。

python3精要(5)-最长公共前缀Trie树相关推荐

  1. 最长公共前缀 字典树 NC55

    class Tire{ public:Tire *children[26];int size;bool isend=false;Tire(){size=0;for(int i=0;i<26;i+ ...

  2. python3.14_leetcode-python3-14. 最长公共前缀

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输出:["flower","flow" ...

  3. 刻意练习:LeetCode实战 -- Task14. 最长公共前缀

    背景 本篇图文是LSGO软件技术团队组织的 第二期基础算法(Leetcode)刻意练习训练营 的打卡任务.本期训练营采用分类别练习的模式,即选择了五个知识点(数组.链表.字符串.树.贪心算法),每个知 ...

  4. 五、Leetcode算法 最长公共前缀

    1.题目 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 输入: ["flower","flow" ...

  5. 【力扣网练习题】最长公共前缀

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  6. leetcode--最长公共前缀--python

    文章目录 题目 题目详情 示例 解题思路 代码 运行结果 最佳方案 题目 题目详情 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 ...

  7. LeetCode实战:最长公共前缀

    题目英文 Write a function to find the longest common prefix string amongst an array of strings. If there ...

  8. 【每日一算法】最长公共前缀

    微信改版,加星标不迷路! 每日一算法-最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["fl ...

  9. Algorithm——最长公共前缀

    一.问题 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1:输入: ["flower","flow& ...

最新文章

  1. 大竹中学2021高考成绩查询,2021年大竹中学升学率高不高?
  2. Spring 基于xml配置方式的AOP
  3. Matlab:绘制简单能量的接收机工作特性曲线(Energy_detection_simulation_ok)
  4. Java Web应用的代码分层最佳实践
  5. Unix/Linux/BSD 它们之间的关系以及各自派系的介绍
  6. SSAS parent/child dimension
  7. python+Django学习资源汇总-更新中
  8. 虚拟空间 配置 服务器,虚拟主机空间可以修改服务器配置吗
  9. UI设计师应该知道的汉字体种类的用途(免费素材)
  10. sql server 分区_SQL Server:锁定设置以用于增强分区功能
  11. SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”
  12. 九度OJ - 题目1481:Is It A Tree?
  13. Reading Note(3)——基于FPGA的动态可重构特性探索新型加速器架构
  14. 防御windows系统激活工具的后门入侵
  15. 武汉代茜2017级计算机科学与技术,毕业论文致谢拾贝(二)
  16. 从ADS到RealView MDK
  17. 玩机器人可以学到什么?
  18. matlab入门学习资料
  19. 文件管理之文件(外存)分配管理 恩赐解脱
  20. 使用 es6 class类创建对象 在其他页面 用vue引用报错 is not a constructor

热门文章

  1. 初接触php,遇到一个低级问题
  2. Mac MacBook Pro的移动硬盘方案
  3. 如何让MFC编写的应用程序(用VC6.0开发)能在别的电脑上运行
  4. js数组对象的常用方法
  5. mongon命令(转)
  6. [今日干货]短视频获得种子用户的途径
  7. 面向对象编程之:封装、继承、多态
  8. Date和TimeZone的关系
  9. codecomb 2091【路径数量】
  10. python 中文识别 不用tesseract_Python——验证码识别 Pillow + tesseract-ocr