Trie implementation
在学习 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的更多相关文章
- 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 ...
- 双数组Trie的一种实现
An Implementation of Double-Array Trie 双数组Trie的一种实现 原文:http://linux.thai.net/~thep/datrie/datrie.htm ...
- Awesome Go
A curated list of awesome Go frameworks, libraries and software. Inspired by awesome-python. Contrib ...
- Aho - Corasick string matching algorithm
Aho - Corasick string matching algorithm 俗称:多模式匹配算法,它是对 Knuth - Morris - pratt algorithm (单模式匹配算法) 形 ...
- Prefix tree
Prefix tree The trie, or prefix tree, is a data structure for storing strings or other sequences in ...
- Go 语言相关的优秀框架,库及软件列表
If you see a package or project here that is no longer maintained or is not a good fit, please submi ...
- go语言项目汇总
Horst Rutter edited this page 7 days ago · 529 revisions Indexes and search engines These sites prov ...
- Radix tree--reference
source address:http://en.wikipedia.org/wiki/Radix_tree In computer science, a radix tree (also patri ...
- Golang优秀开源项目汇总, 10大流行Go语言开源项目, golang 开源项目全集(golang/go/wiki/Projects), GitHub上优秀的Go开源项目
Golang优秀开源项目汇总(持续更新...)我把这个汇总放在github上了, 后面更新也会在github上更新. https://github.com/hackstoic/golang-open- ...
随机推荐
- [置顶] ProcessOn:划时代性的在线作图工具
ProcessOn是一款专业作图人员的社交网络,这里汇聚很多业界专家.学者,同时他们分享的作品又形成一个庞大的知识图库,你在学习专业知识的同时还可以结交一些志同道合的新朋友. ProcessOn核心设 ...
- Xcode 4.1~4.6 + iOS 5、iOS 6免证书(iDP)开发+真机调试+生成IPA全攻略
原创文章,欢迎分享:未经许可,不得转载:版权所有,侵权必究 开发环境使用的是目前为止最新的稳定版软件:Mac OS X Lion 10.7 + Xcode 4.1 目前Xcode 4.2 Previe ...
- js中的刷新方法
刷新并清除缓存: location.reload(true); 返回上一页并刷新: history.go(-1); location.reload(true); 子页面刷新父页面: self.open ...
- log file sync等待超高一例
这是3月份某客户的情况,原因是server硬件故障后进行更换之后,业务翻译偶尔出现提交缓慢的情况.我们先来看下awr的情况. 我们能够看到,该系统的load profile信息事实上并不高,每秒才21 ...
- java写文件时,输出不完整的原因以及解决方法
在java的IO体系中,写文件通常会用到下面语句 BufferedWriter bo=new BufferedWriter(new FileWriter("sql语句.txt")) ...
- 关于Lambda表达式访问外部变量
在<C#高级编程>一书中提到通过Lambda表达式可以访问Lambda表达式块外部的变量 ,这是一个很好的功能(类似Js中的 闭包).但是如果没有正确的使用,会非常危险. 比如下面的事例中 ...
- css3 选择器的比较(一) -- 以字符串开头
一. 以“字符串”开头 两种用法的区别是: a. 以字符串开头,需要用"-"分割开, b. 以字符串开头,不需要任何分隔符 1. 资料 a) b) 2. html代码 < ...
- Asp.net的对Excel文档的导入导出操作
刚刚初入职场,在休闲的时间写下了项目中用到的对Excel文档操作的方法以及总结,多的不说,直接上代码 public static void CreateExcel(DataSet ds, string ...
- LDAP基础
超级好的LDAP文章: Linux下基于LDAP统一用户认证的研究 : http://chenguang.blog.51cto.com/350944/285602利用LDAP实现windows和Lin ...
- SQL Server数据库连接字符串整理
1.sql验证方式的 Data Source=数据源;Initial Catalog= 数据库名;UserId=sql登录账号;Password=密码; Eg: Data Source=.;Initi ...