在学习 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. css Block formatting context BFC

    w3c关于BFC解释: http://www.w3.org/TR/CSS21/visuren.html#block-formatting Mdn描述: A block formatting conte ...

  2. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  3. 雕爷牛腩这样判断零售未来-搜狐IT

    雕爷牛腩这样判断零售未来-搜狐IT 雕爷牛腩这样判断零售未来

  4. transition与visibility与display

    http://www.zhangxinxu.com/wordpress/2013/05/transition-visibility-show-hide/ 术语解释是: visibility: 离散步骤 ...

  5. drupal7中CKEditor开启上传图片功能

    在drupal建站中,所见即所得编辑器提供了友好的界面.也提高开发效率,而CKEditor是一款非常不错的编辑器.定制性相当高,在这推荐给大家. CKEditor和其它模块(IMCE)搭配下在文字排版 ...

  6. Linux编程---套接字

    网络相关的东西差点儿都是建立在套接字之上.所以这个内容对于程序猿来说还是蛮重要的啊. 事实上套接字也就是一个特殊的设备文件而已,我始终不能明确为什么要叫套接字.这么个奇怪的名字.只是还是就这样算了吧. ...

  7. 利用虚拟光驱实现 将WINDOWS文件供虚拟机中的UBUNTU共享

    此方法只能实现(至少目前我发现只能这样)将文件传递给虚拟机中的ubuntu 中,供ubuntu系统阅读,拷贝等,但不能将ubuntu中的数据传递给windows. 即:每次更新windows的数据到u ...

  8. 在windows后台调用webservice

    1.首先要创建个webservice,然后再webservice写一个方法如图 2.然后将WebService1.asmx 在浏览器中浏览会出现如图所示(该地址很重要,复制此地址在下边程序中要用到) ...

  9. BZOJ 1021: [SHOI2008]Debt 循环的债务( dp )

    dp(i, j, k)表示考虑了前i种钱币(从小到大), Alice的钱数为j, Bob的钱数为k, 最小次数. 脑补一下可以发现, 只有A->B.C, B->A.C, C->A.B ...

  10. leetcode Longest Palindromic Substring python

    class Solution(object): def longestPalindrome(self, s): """ :type s: str :rtype: str ...