字典树模板(数组实现和指针实现)
来源:互联网 发布:房屋设计图平面图软件 编辑:程序博客网 时间:2024/05/21 07:10
字典树的指针实现:
C++ Code
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;
}
#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
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;
}
#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;
}
阅读全文
0 0
- 字典树模板(数组实现和指针实现)
- HDOJ1671 字典树入门题+模板程序(指针实现)
- HDOJ5687 字典树模板,数组实现
- 【字典树模板(数组实现)】HDU 1251 统计难题
- poj 2001 字典树入门题(数组实现模板)
- 字典树数组实现
- 字典树的指针模板与数组模板
- 实现二分查找(指针和数组)
- 【Trie】Trie字典树模板 静态指针池、数组写法
- 数组字典树模板
- HDU1671用指针实现的字典树
- HDU1274 Hat’s Words(字典树 数组实现)
- UVA 11732 "strcmp()" Anyone? 字典树(数组实现)
- HDU1075静态数组实现字典树
- 使用数组和模板实现的队列
- C语言二分查找法(指针和数组实现)
- 指针和对象的数组实现
- 用指针和数组实现字符串复制
- Distutils发布C++扩展的Python模块
- 使用Scikit-learn实现基于内存的协同过滤算法(使用movieLens数据集)
- 上帝造题的7分钟2
- centos7 安装mysql步骤
- idea 创建java web项目(maven管理的SSH)
- 字典树模板(数组实现和指针实现)
- (UVA
- bzoj 1632: [Usaco2007 Feb]Lilypad Pond SPFA
- 任务、进程、线程之间的区别
- openwrt各种奇怪问题总结(对C++依赖)
- POJ
- 关于ip
- 匈牙利游戏之A*版
- 随笔