字典树模板(数组实现和指针实现)

来源:互联网 发布:房屋设计图平面图软件 编辑:程序博客网 时间:2024/05/21 07:10

字典树的指针实现:

 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], 0sizeof(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], 0sizeof(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;
}


原创粉丝点击