在学习 Aho - Corasick Automation Algorithm 之前,先学习一下,Trie 的实现过程:

  The name trie comes from its use for retrieval (检索) , 通常读“/ tri /" ;

  Trie is a special kind of tree ;

  What  is  trie  ?

  给你七八百个单词,然后随意给你一个单词,问一下这个单词是不是在这七八百个单词之中,if we stroe these words in liner array , it will be very unefficient (需要很大的内存,同时查找也需要很长时间). The model of trie more efficient ;

  reading more on Wikipedia page .

  通过下面的图,直观的介绍一下,数据在 Trie 中的存储方式:

  

A trie for key "A", "to", "tea", "ted", "ten", "i", "in", and "inn".

这样很 easy 避免了几个单词中具有重复部分占用内存空间的情况;

we store only the individual characters of the string key in the nodes

each node can have multiple children , 从 a 到 z (特殊考虑一下,全部为小写,不包含数字字符)同样造成了空间大量的浪费;不过没关系,我们可以找到某种方法,把那些不存在字符的结点给 detele 掉 即可 ;

在 Trie 中可以实现插入、删除、查找等功能,实现的功能不同,结点中的数据成员有所不同;

HDU中有一道题是关于 Trie 的运用 ,大致描述一下题意:

  输入N组电话号码,在输入的过程中,如果出现包含的情况,最后结果就输出NO,否则输出YES ;

  第一次用 Hash table 做的,果然不出所料,以超时而放弃,然后换为Trie来解答,结果没有delete掉不用的内存空间,结果内存超了,最后用了个递归,把不用的内存全部给delete掉,OK了!

下面给出这道题的代码,其中也有许多细节需要注意的地方:

#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
using namespace std ; struct Node {
bool flag ;
Node *next[11] ;
} ; Node* new_Node() {
Node *root = new Node ;
root->flag = false ;
memset(root->next,NULL,sizeof(root->next));
return root ;
}
bool tag ; void deletee( Node *r ) {
for(int i = 0 ; i < 10 ; i++) {
if(r->next[i] != NULL)
deletee(r->next[i]) ;
delete(r->next[i]) ;
}
} void Construct_trie(Node *point , char *s) {
int len = strlen(s) ;
for(int i = 0 ; i < len ; i++) {
if(point->flag || (point ->next[s[i]-'0'] != NULL && i == len - 1))
tag = false ;
if(point->next[s[i]-'0'] == NULL)
point->next[s[i]-'0'] = new_Node() ;
point = point->next[s[i]-'0'] ;
}
point->flag = true ;
} int main() {
int m ;
scanf("%d", &m) ;
while(m--) {
int n ;
scanf("%d",&n) ;
Node *root = new_Node() ;
tag = true ;
while(n--) {
char s[10005] ;
scanf("%s",&s) ;
Construct_trie(root,s);
}
if(tag)
printf("YES\n") ;
else
printf("NO\n") ;
deletee(root) ;
}
return 0 ;
}

Trie implementation的更多相关文章

  1. An Implementation of Double-Array Trie

    Contents What is Trie? What Does It Take to Implement a Trie? Tripple-Array Trie Double-Array Trie S ...

  2. 双数组Trie的一种实现

    An Implementation of Double-Array Trie 双数组Trie的一种实现 原文:http://linux.thai.net/~thep/datrie/datrie.htm ...

  3. Awesome Go

    A curated list of awesome Go frameworks, libraries and software. Inspired by awesome-python. Contrib ...

  4. Aho - Corasick string matching algorithm

    Aho - Corasick string matching algorithm 俗称:多模式匹配算法,它是对 Knuth - Morris - pratt algorithm (单模式匹配算法) 形 ...

  5. Prefix tree

    Prefix tree The trie, or prefix tree, is a data structure for storing strings or other sequences in ...

  6. Go 语言相关的优秀框架,库及软件列表

    If you see a package or project here that is no longer maintained or is not a good fit, please submi ...

  7. go语言项目汇总

    Horst Rutter edited this page 7 days ago · 529 revisions Indexes and search engines These sites prov ...

  8. Radix tree--reference

    source address:http://en.wikipedia.org/wiki/Radix_tree In computer science, a radix tree (also patri ...

  9. Golang优秀开源项目汇总, 10大流行Go语言开源项目, golang 开源项目全集(golang/go/wiki/Projects), GitHub上优秀的Go开源项目

    Golang优秀开源项目汇总(持续更新...)我把这个汇总放在github上了, 后面更新也会在github上更新. https://github.com/hackstoic/golang-open- ...

随机推荐

  1. poj2608---几个字母映射到同一个数字

    #include <stdio.h> #include <stdlib.h> #include<string.h> ]={,,,,,,,,,,,,,,,,,,,,, ...

  2. saiku之固定维度(必选维度)

    工作中遇到的问题,记录下来方便以后查找. 在saiku中如何设定固定维度? 找到WorkspaceDropZone.js文件,在synchronize_query: function(){}方法中的“ ...

  3. 微信开发-Jssdk调用分享实例

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO ...

  4. Java中使用Observer接口和Observable类实践Observer观察者模式

    在Java中通过Observable类和Observer接口实现了观察者模式.实现Observer接口的对象是观察者,继承Observable的对象是被观察者. 1. 实现观察者模式 实现观察者模式非 ...

  5. SQL 语句优化—— (二) 索引的利用

    索引是与表或视图关联的磁盘上结构,可以加快从表或视图中检索行的速度.索引包含由表或视图中的一列或多列生成的键.与书中的索引一样,数据库中的索引使您可以快速找到表或索引视图中的特定信息.索引包含从表或视 ...

  6. Filemanager 的使用

    filemanager的使用包括: 1.创建文件夹 2.删除文件夹 3.写入文件 4.复制文件 5.移动文件 6.删除文件​ 一.创建文件夹​ 首先宏的定义一个字符串作为地址的​来获取当前的docum ...

  7. WCF编写时候的测试

    1右击WCF创建到使用到发布这篇文章中的类库项目中的接口类实现文件添加断点 2右击WCF创建到使用到发布这篇文章中的WCF服务网站设为启动项并允许 3右击WCF创建到使用到发布这篇文章中的WPF项目调 ...

  8. Ubuntu下安装Mysql并使用

    一.在Ubuntu终端中输入 sudo apt-get install mysql-server 二.在安装中要设置root密码,自己输入即可,但必须要记住. 三.安装后可以使用命令检测是安装成功 s ...

  9. fiddler--firefiox代理

    修改端口:修改后重启才能生效

  10. 初识C(1)----与C基本无关的开篇

    1.啥叫编程 编程乃编写程序的简称,所以要想知道啥叫编程,首先要清楚什么是程序(Program). 普及一点计算机小知识:从根本上说,计算机是由数字电路组成的运算机器,处理的数字也仅限于0和1组成的数 ...