字典树模板(数组实现和指针实现)
2017-08-15 08:41
176 查看
字典树的指针实现:
C++ Code
字典树的数组实现:
C++ Code
C++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | ///这里以输入字符串后,查询字符串出现的次数为例 #include<bits/stdc++.h> #define MAX 26 using namespace std; typedef struct TrieNode ///Trie节点声明 { int num; ///数据域,可根据需要进行改变 struct TrieNode *next[MAX]; ///儿子分支 } Trie; int ans = -1; char b[15]; void insert1(Trie *root, char *s) { if(root == NULL || *s == '\0') return ; Trie *p = root; while(*s != 0) { if(p->next[*s - 'a'] == NULL) { ///如果当前节点还不存在,就建立新的节点 Trie *temp = (Trie *)malloc(sizeof(Trie)); for(int i = 0; i < MAX; i++) temp->next[i] = NULL; temp->num = 0; p->next[*s - 'a'] = temp; p = p->next[*s - 'a']; } else p = p->next[*s - 'a']; s++; } p->num++; } bool Find(Trie *rt, char *s) { int len = strlen(s); for(int i = 0; i < len; i++) { int c = s[i] - 'a'; if(!rt->next[c]) return false; rt = rt->next[c]; } if(rt->num) return true; return false; } int main() { int n; char a[15]; Trie *root = (Trie *)malloc(sizeof(Trie)); for(int i = 0; i < MAX; i++) root->next[i] = NULL; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%s", a); insert1(root, a); } while(~scanf("%s", a)) { if(Find(root, a)) puts("exist"); else puts("none"); } return 0; } |
C++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | ///这里以输入字符串后,查询字符串出现的次数为例 #include <bits/stdc++.h> #define maxn 1000000 using namespace std; struct Trie { int next[26];///表示下一个节点是否存在, int val;///节点的数据域,可根据需要进行改变 } tree[maxn]; int nxt;///nxt表示节点的编号 char str[26];///存储输入的字符串 int add() { ///建立新的节点 memset(&tree[nxt], 0, sizeof(Trie)); return nxt++; } void Insert(char *s)///插入以及建树 { int rt = 0, len = strlen(s); for(int i = 0; i < len; i++) { int c = s[i] - 'a'; if(!tree[rt].next[c]) { ///如果树中还不存在这个节点,就开辟新的节点 tree[rt].next[c] = add(); } rt = tree[rt].next[c]; } tree[rt].val++;///维护数据域(这个表示出现的次数) } bool Find(char *s) { ///查找字符串是否存在 int rt = 0, len = strlen(s); for(int i = 0; i < len; i++) { int c = s[i] - 'a'; if(!tree[rt].next[c]) return false; rt = tree[rt].next[c]; } if(tree[rt].val) return true; return false; } int main() { int T; memset(&tree[0], 0, sizeof(Trie)); nxt = 1; scanf("%d", &T); while(T--) { scanf("%s", str); Insert(str); } while(~scanf("%s", str)) { if(Find(str)) puts("exist"); else puts("none"); } return 0; } |
相关文章推荐
- HDU1251 统计难题(字典树模板题--数组实现+指针实现)
- [模板] 树堆 - Treap的指针和数组实现及一些例题
- poj 2001 字典树入门题(数组实现模板)
- 字典树的指针模板与数组模板
- HDOJ5687 字典树模板,数组实现
- 【字典树模板(数组实现)】HDU 1251 统计难题
- HDOJ1671 字典树入门题+模板程序(指针实现)
- 将两个排好序的序列合并成一个(指针和数组分别实现)
- 用c++模板实现 线性表(数组表示)
- AC自动机模板(数组+指针)hdu2222
- php 数组的指针操作实现代码
- 基于数据成员是指向一个数组的指针来实现的list
- C指针实现找出一个数组中的最大值和次大值
- 一个智能指针模板的实现及应用
- HDU1274 Hat’s Words(字典树 数组实现)
- strcat,strcpy,strcmp,strlen4个常用字符串处理函数的数组与指针简单实现方法~
- 从零开始学C++之模板(二):类模板、Stack的类模板实现(自定义链栈方式,自定义数组方式)
- C语言通过指针和数组实现字符串倒序
- 约瑟夫环问题的PHP实现 使用PHP数组内部指针操作函数
- 使用数组和模板实现的队列