字典树 模板



Code
 1//字典树模板
 2#include <iostream>
 3using namespace std;
 4const int kind=26;
 5struct node
 6{
 7    int i,count;
 8    node *next[kind];
 9    node()
10    {
11        count=1;
12        for(i=0;i<kind;i++)
13            next[i]=NULL;
14    }

15}
;
16void insert(node *root,char *word)
17{
18    int i=0,branch;
19    node *local=root;
20    if(local==NULL)
21    {
22        local=new node();
23        root=local;
24    }

25    while(word[i])
26    {
27        branch=word[i]-'a';
28        if(local->next[branch])
29            local->next[branch]->count++;
30        else
31            local->next[branch]=new node();
32        i++;
33        local=local->next[branch];
34    }

35}

36int search(node *root,char *word)
37{
38    int i=0,branch,ans;
39    node *local=root;
40    
41    if(local==NULL)
42        return 0;
43    while(word[i])
44    {
45        branch=word[i]-'a';
46        if(!local->next[branch])
47            return 0;
48        i++;
49        local=local->next[branch];
50        ans=local->count;
51    }

52    return ans;
53}

54int main()
55{
56    char str[12];
57    node *root=new node;
58    while(gets(str) && strcmp(str,""))
59        insert(root,str);
60    while(scanf("%s",str)!=EOF)
61        printf("%d\n",search(root,str));
62    return 0;
63}

 

转载于:https://www.cnblogs.com/Knuth/archive/2009/09/08/1562612.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值