基于trie树做一个ac自动机


#!/usr/bin/python
# -*- coding: utf-8 -*- class Node:
def __init__(self):
self.value = None
self.children = {} # children is of type {char, Node}
self.fre = 0
self.father = None
self.fail = None def CMP(a, b):
return b.fre - a.fre class Trie:
def __init__(self):
self.root = Node()
self.choose = []
self.__OpenCorrect__ = 0 def insert(self, key): # key is of type string
# key should be a low-case string, this must be checked here!
node = self.root
for char in key:
if char not in node.children:
child = Node()
node.children[char] = child
child.value = char
child.father = node
node = child
else:
node = node.children[char]
# node.value = key
node.fre += 1 def find_node(self, string):
res_node = self.root
try:
for i in string:
res_node = res_node.children[i]
except:
res_node = None
return res_node def buildac3(self):
queuelist = []
queuelist.append(self.root)
while len(queuelist) > 0:
temp = queuelist.pop()
p = None
for k, v in temp.children.items():
if temp == self.root:
temp.children[k].fail = self.root
else:
p = temp.fail
while p is not None:
if p.children.has_key(k):
temp.children[k].fail = p.children[k]
break
p = p.fail
if p is None:
temp.children[k].fail = self.root
queuelist.append(temp.children[k]) def acfind(self, content):
count = 0
content2 = content
while len(content2) > 1:
p = self.root
result = []
startWordIndex = 0
endWordIndex = -1
currentPosition = 0
while currentPosition < len(content2):
word = content2[currentPosition]
while p.children.has_key(word) == False and p != self.root:
p = p.fail
if p.children.has_key(word):
if p == self.root:
startWordIndex = currentPosition
p = p.children[word]
else:
p = self.root
if p.fre > 0 and currentPosition - startWordIndex < len(content) - 1:
result.append((startWordIndex + count, currentPosition + count))
currentPosition += 1
for i in result:
print content[i[0]:i[1] + 1]
print result
count += 1
content2 = content2[1:] if __name__ == '__main__':
trie = Trie()
trie.__OpenCorrect__ = 1
trie.insert("she")
trie.insert("he")
trie.insert("her")
trie.insert("hers")
trie.buildac3()
# print trie.find_node('sw')
# print trie.root.children['s'].children['h'].fail.value
print trie.acfind('shers')

基于trie树做一个ac自动机的更多相关文章

  1. 基于trie树的具有联想功能的文本编辑器

    之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...

  2. 基于thinkphp5框架做一个可以区别开发、测试、生产三种环境的配置加载

    在日常的开发测试中我们经常会遇到本地开发和测试或者线上配置参数不同的场景,必要你要是使用一个三方的支付,它的本地测试和线上的key值或者账号存在不同.最基本的做法是本地用测试参数,提交到测试的时候再改 ...

  3. BZOJ2434 [NOI2011] 阿狸的打字机 【树链剖分】【线段树】【fail树】【AC自动机】

    题目分析: 画一下fail树,就会发现就是x的子树中属于y路径的,把y剖分一下,用线段树处理 $O(n*log^2 n)$. 代码: #include<bits/stdc++.h> usi ...

  4. [HNOI2004]L语言 trie树? Ac自动机? hash!!

    题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构成.一个单词W也是由若干小写字母构成.一个字典D是若干个单词的 ...

  5. 小菜鸟 菜谈 KMP->字典树->AC自动机->trie 图 (改进与不改进)

    本文的主要宗旨是总结自己看了大佬们对AC自动机和trie 图 的一些理解与看法.(前沿:本人水平有限,总结有误,希望大佬们可以指出) KMP分割线--------------------------- ...

  6. AC自动机——1 Trie树(字典树)介绍

    AC自动机——1 Trie树(字典树)介绍 2013年10月15日 23:56:45 阅读数:2375 之前,我们介绍了Kmp算法,其实,他就是一种单模式匹配.当要检查一篇文章中是否有某些敏感词,这其 ...

  7. 【AC自动机】【字符串】【字典树】AC自动机 学习笔记

    blog:www.wjyyy.top     AC自动机是一种毒瘤的方便的多模式串匹配算法.基于字典树,用到了类似KMP的思维.     AC自动机与KMP不同的是,AC自动机可以同时匹配多个模式串, ...

  8. BZOJ 3172: [Tjoi2013]单词 [AC自动机 Fail树]

    3172: [Tjoi2013]单词 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 3198  Solved: 1532[Submit][Status ...

  9. 字符串 --- KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组

    涉及到字符串的问题,无外乎这样一些算法和数据结构:自动机 KMP算法 Extend-KMP 后缀树 后缀数组 trie树 trie图及其应用.当然这些都是比较高级的数据结构和算法,而这里面最常用和最熟 ...

随机推荐

  1. HTML 事件(一) 事件的介绍

    本篇主要介绍HTML中的事件知识:事件相关术语.DOM事件规范.事件对象. 其他事件文章 1. HTML 事件(一) 事件的介绍 2. HTML 事件(二) 事件的注册与注销 3. HTML 事件(三 ...

  2. webpack+react+redux+es6开发模式

    一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...

  3. WebApi - 路由

    这段时间的博客打算和大家一起分享下webapi的使用和心得,主要原因是群里面有朋友说希望能有这方面的文章分享,随便自己也再回顾下:后面将会和大家分不同篇章来分享交流心得,希望各位多多扫码支持和点赞,谢 ...

  4. spring源码分析之@ImportSelector、@Import、ImportResource工作原理分析

    1. @importSelector定义: /** * Interface to be implemented by types that determine which @{@link Config ...

  5. YII 2.x 模板文件的 beginBlock、beginContent、beginCache

    echo '-----------beginBlock--------------------- <br />'; $this->beginBlock('block1', false ...

  6. hbase协处理器编码实例

    Observer协处理器通常在一个特定的事件(诸如Get或Put)之前或之后发生,相当于RDBMS中的触发器.Endpoint协处理器则类似于RDBMS中的存储过程,因为它可以让你在RegionSer ...

  7. WebAPI 2参数绑定方法

    简单类型参数 Example 1: Sending a simple parameter in the Url [RoutePrefix("api/values")] public ...

  8. Linux上课笔记--随手记Linux命令

    初次接触Linux就是感觉这系统不够友好不够人性化,因为首先接触电脑就是win,图形化界面什么操作都可以清晰看到.随着更多的接触越来越发现Linux的强大,虽然我只是一个小白,可我就是爱上他了.现在就 ...

  9. 端盘子的服务生到月薪一万五的IT精英,你能相信吗

    一直以来,我都觉得自己不是一个有故事的人. 以前的我,是个乖宝宝,对父母言听计从,特别内向,甚至一度感觉到自卑.不上学之后,我干过送货员,去工地除泥搬砖,当过油漆工,去过工厂,还去饭店当过端盘子的服务 ...

  10. 论C#之多继承

    C#多继承的讨论似乎是个古老的问题了,但今天本文要向大家展示的C#多继承可能是大家闻所未闻见所未见的,甚至是发明C#语言的人也不曾想到我会这样去写代码,并且自得其乐. 说起多继承,首先大家可以想想这个 ...