HDU 1251 裸的字典树、入门题

本文介绍裸字典树的基本概念及其在C++中的简单实现方式,包括建立、查找、插入和删除等核心操作。通过实例演示,帮助读者理解字典树在字符串匹配和数据检索中的应用。

裸的字典树还是挺简单的、

四个基本操作建立、查找、插入、删除

建立新结点我是用的c++中 new操作、当然也可以用malloc,都方便

不过指针阿、地址阿、这其中关系什么的我貌似还不是很清楚阿、

因为刚开始我的头结点也是定义的指针、然后程序就炸了、我不清楚原因呢、

有待弄清楚、

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cstring>
 6 typedef struct node
 7 {
 8     int cnt;
 9     struct node *next[26];
10     node()
11     {
12         cnt=0;
13         memset(next,0,sizeof(next));
14     }
15 }t;
16 node root;        //头结点 
17 void save(char *s)
18 {
19     int len=strlen(s);
20     if(len==0)    return;
21     node *p=&root;
22     node *tmp=NULL;
23     for(int i=0;i<len;++i){
24         if(p->next[s[i]-'a']==NULL){
25             tmp=new struct node;
26             p->next[s[i]-'a']=tmp;
27         }
28         p=p->next[s[i]-'a'];
29         p->cnt++;
30     }
31 }
32 void findtree(char *s)
33 {
34     struct node *p=&root;
35     int i,l=strlen(s);
36     for(int i=0;i<l;++i){
37         if(p->next[s[i]-'a']==NULL){
38             printf("0\n");
39             return;
40         }
41         p=p->next[s[i]-'a'];
42     }
43     printf("%d\n",p->cnt);
44     return;
45 }
46 int main()
47 {
48     char s[15];
49     while(gets(s)&&s[0]!=0)    save(s);
50     while(~scanf("%s",s))
51         findtree(s);
52     return 0;
53 }

 

转载于:https://www.cnblogs.com/sasuke-/p/5354471.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值