HDU 1251 统计难题 (Trie树——字符串算法)
来源:互联网 发布:java中的getclass方法 编辑:程序博客网 时间:2024/05/16 17:51
在vj上拉了一些题,有一题它需要用到trie树。
题目大意是这样的:给一个单词表(只有小写字母组成,不会有重复的单词出现),让我们统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
Sample Input
bananabandbeeabsoluteacmbabbandabc
Sample Output
2310
这种前缀的问题,在我不知道trie树的时候,就暴力嘛(暴力出真知/斜眼笑)!每个字符串从单词表开始到结尾依次判断,但是时间复杂度就很大了,这个复杂度有O(n^2),如果n很大,这显然是不好的。
所以介绍trie算法。
参考一位巨巨的文章:
http://blog.csdn.net/hackbuteer1/article/details/7964147
prie树:
特征:
(1)根节点设为空,其他的每个节点只包含一个字符。
(2)从根节点到树中的某一个节点,路上经过的字符从上到下连接起来,为该节点对应的字符串。
举例:
bananabandbeeabsoluteacmNULL ->a->b->s->o->l->u->t->e ->c->m ->b->a->n->a->n->a ->d
因为没有找到合适的画树图软件,就用上面的表示,从NULL开始,同一列的为同一层。
Trie树结点结构体声明如下。
typedef struct Trie_node { int count; // 统计从根节点到此结点的字符串出现的次数 struct Trie_node* next[26]; // 指向各个子树的指针,字母26个 bool exist; // 标记此结点是否构成单词}TrieNode , *Trie;
创建新结点:
用malloc开辟一个地址,将此结点的count置0,子树置空,构成单词标记置false。
TrieNode* createTrieNode(){ TrieNode* node = (TrieNode *)malloc(sizeof(TrieNode)); node->count = 0; node->exist = false; memset(node->next, NULL, sizeof(node->next)); return node;}
创建单词表:
这里就开始了trie树的build了,注意从根结点开始进行建树,过程通过字符的id来判断子树是否被创建过,如果没有创建过,就malloc一个子结点,然后到达子节点,依次往下。
举例:
注意括号中为count数
单词1:banana:
NULL ->b(1)->a(1)->n(1)->a(1)->n(1)->a(1) 此单词构建完成,最后一个a结点的exit置为true。
单词2:band:
NULL->b(2)->a(2)->n(2)->a(1)->n(1)->a(1) ->d(1)此单词构建完毕,发现结点b,a,n的count数变成了2,并且结点d的exit置为true。
代码如下:
void Trie_insert(Trie root, char* word){ Trie node = root; char *p = word; int id; while( *p ) { id = *p - 'a'; if(node->next[id] == NULL) { node->next[id] = createTrieNode(); } node = node->next[id]; ++p; node->count += 1; } node->exist = true;}
查询:
从NULL开始,取s[i],依次判断node->next[s[i]-‘a’]是否为空,如果为空,说明单词中没有要查询的字符串的字符,则返回0;否则就取最后一个结点的count。
就上面的例子来看,即现在的trie树为:
NULL->b(2)->a(2)->n(2)->a(1)->n(1)->a(1) ->d(1)
我们查询字符串s为“ba”:
1.先是‘b’:从NULL开始发现下一个结点是b,继续
2.最后是‘a’:从b开始发现下一个结点是a,则返回2。
单词表是banana 和 band , 查询的是“ba”,的确是两个单词的前缀。
int Trie_search(Trie root, char* word){ Trie node = root; char *p = word; int id; while( *p ) { id = *p - 'a'; node = node->next[id]; ++p; if(node == NULL) return 0; } return node->count;}
完整代码如下:
#include<cstdio>#include<cstring>#include<iostream>#include<malloc.h>using namespace std;/*声明结构体*/typedef struct Trie_node{ int count; struct Trie_node* next[26]; bool exist;}TrieNode, *Trie;/*创建新结点*/TrieNode* createTrieNode(){ TrieNode* node = (TrieNode *)malloc(sizeof(TrieNode)); node->count = 0; node->exist = false; memset(node->next, NULL, sizeof(node->next)); return node;}/*建trie树*/void Trie_insert(Trie root, char* word){ Trie node = root; char *p = word; int id; while( *p ) { id = *p - 'a'; if(node->next[id] == NULL) { node->next[id] = createTrieNode(); } node = node->next[id]; ++p; node->count += 1; } node->exist = true;}/*查询*/int Trie_search(Trie root, char* word){ Trie node = root; char *p = word; int id; while( *p ) { id = *p - 'a'; node = node->next[id]; ++p; if(node == NULL) return 0; } return node->count;}int main(){ Trie root = createTrieNode(); char str[12]; bool flag = false; while(gets(str)) { if(flag) printf("%d\n", Trie_search(root, str)); else{ if(strlen(str) != 0) { Trie_insert(root, str); }else{ flag = true; } } } return 0;}
- HDU 1251 统计难题 (Trie树——字符串算法)
- HDU 1251 统计难题 (字符串-Trie树)
- hdu 1251 统计难题(trie树)
- hdu 1251 统计难题 (trie树)
- HDU 1251 ——统计难题(Trie 入门)
- hdu 1251 统计难题//trie树
- hdu 1251 统计难题 trie树
- HDU 1251 统计难题(字典树Trie)
- HDU 1251 统计难题 (Trie树)
- hdu 1251 统计难题 【Trie树】
- HDU 1251 统计难题 trie树
- hdu 1251 统计难题 Trie树
- HDU 1251 - 统计难题 (Trie)
- HDU 1251 统计难题 (Trie)
- HDU 1251 统计难题 字典树入门(Trie)
- HDU 1251 统计难题(trie树入门)
- (字典树Trie Tree)HDU 1251 统计难题
- hdu 1251统计难题(trie)
- 学习淘淘商城第二十五课(CMS内容管理系统表设计及内容工程搭建)
- 权限校验过滤器
- linux用户与用户组管理
- springmvc之自定义视图BeanNameViewResolver
- 算法——猴子吃桃问题
- HDU 1251 统计难题 (Trie树——字符串算法)
- 在eclipse中用maven创建web项目
- 颜色分类
- java内存分配
- 2014-Network In Network
- struts2通配符及Action开发的三种方式
- POJ-3977-折半枚举,二分
- Thread 的stop方法过时Thread already start解决方法
- 解决RabbitMQ发送数据为空字符串