HDU 1247 Hat’s Words Trie题解
来源:互联网 发布:中咨数据有限公司待遇 编辑:程序博客网 时间:2024/06/04 18:53
使用Trie的insert函数,主要是要灵活修改search函数,使得其可以快速搜索hat word。
思路:
1 先搜索一个word的前缀有没有在Trie树中找到,如果找到,保存这个Node,方便下面继续搜索, 就搜索余下的能不能是Trie树上的一个单词,如果找到,那么就是hat word了。
2 如果没找到,那么就沿着刚刚搜索到的前缀单词的节点继续往下搜,这样就可以加速程序,不用每次重复搜索。
3 如果沿着Trie树已经走到word的叶子节点了,那么就结束这个word的搜索了。
实现好这些思路,那么速度是很快的。
#include <stdio.h>#include <string.h>const int MAX_N = 50001;const int ARR_SZIE = 26;char dict[MAX_N][100];//char dict[MAX_N][15]也AC,10就REint N;struct Node{int n;Node *arr[ARR_SZIE];Node():n(0){for (int i = 0; i < ARR_SZIE; i++){arr[i] = NULL;}}};void delTrie(Node *trie){if (trie){for (int i = 0; i < ARR_SZIE; i++){if (trie->arr[i]) delete trie->arr[i];}delete trie;}}Node *Trie;void insert(char *ch){Node *pCrawl = Trie;int len = strlen(ch);for (int i = 0; i < len; i++){int index = ch[i]-'a';if (!pCrawl->arr[index]) pCrawl->arr[index] = new Node;pCrawl = pCrawl->arr[index];}pCrawl->n++;}int pathLen;Node *searchHat(Node *rt, char *chs, int len){Node *pCrawl = rt;for (int i = 0; i < len; i++){int index = chs[i] - 'a';if (!pCrawl->arr[index]) return NULL;pCrawl = pCrawl->arr[index];pathLen++;if (pCrawl->n) return pCrawl;}if (pCrawl->n) return pCrawl;return NULL;}bool search(Node *rt, char *chs, int len){Node *pCrawl = rt;for (int i = 0; i < len; i++){int index = chs[i] - 'a';if (!pCrawl->arr[index]) return false;pCrawl = pCrawl->arr[index];}return pCrawl->n > 0;}int main(){Trie = new Node;N = 0;while (gets(dict[N])) insert(dict[N++]);for (int i = 0; i < N; i++){int len = strlen(dict[i]);pathLen = 0;Node *node = Trie;while (pathLen < len){node = searchHat(node, dict[i]+pathLen, len-pathLen);if (pathLen < len && node){if (search(Trie, dict[i]+pathLen, len-pathLen)){puts(dict[i]);break;}}}}delTrie(Trie);return 0;}
1 1
- HDU 1247 Hat’s Words Trie题解
- hdu 1247 Hat’s Words Trie树
- HDU 1247 Hat's words(Trie)
- hdu 1247 Hat’s Words(dfs+trie)
- HDU -- 1247 Hat’s Words (Trie 树)
- hdu 1247 Hat’s Words trie
- HDU 1247 Hat’s Words // Trie, 枚举
- hdu 1247 Hat’s Words(Trie)
- [hdu 1247]Hat’s Words [Trie 图]
- Hdu 1247 Hat's Words(Trie树)
- HDU 1247 Hat’s Words (Trie)
- HDU 1247 Hat’s Words && Trie(字典树)
- HDU 1247 Hat’s Words(字典树Trie)
- HDU 1247 Hat's words(字典树Trie)
- HDU ACM 1247-Hat’s Words-字典树(Trie)
- HDU 1247 Hat’s Words (Trie·指针实现)
- hdu 1247 Hat’s Words(Trie树入门)
- hdu 1247 Hat’s Words Trie树(+测试数据)
- 《GOF设计模式》—桥接(BRIDGE)—Delphi源码示例:共享Implementor对象
- 类库、子系统、框架与架构
- 软件架构思想
- C++ STL遇到的几个问题
- 软件架构概念
- HDU 1247 Hat’s Words Trie题解
- hiho第五周
- HDU 4006 The kth great number (基本算法-水题)
- 《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:组合接口
- 软件架构视图
- 也许这、梦想起航的地方
- C++ primer(第五版) 练习 6.5 个人code
- 代码风格
- Java中迭代器使用小结