字典树
来源:互联网 发布:上海黑马程序员地址 编辑:程序博客网 时间:2024/05/22 08:13
#include<stdio.h> #include<string.h> #include<stdlib.h> bool is_phone;/*判断是否能成功拨打号码*/ struct node { bool flag;/*在数字串最后标记*/ struct node *next[10]; }; struct node *root; struct node *newset()/*新建结点*/ { struct node *p=(struct node *)malloc(sizeof(struct node)); for(int i=0;i<10;i++) { p->next[i]=NULL; } p->flag=false;/*初始化为false*/ return p; } void insert(char *str)/*建立字典树*/ { struct node *p; p=root; int len=strlen(str); for(int i=0;i<len;i++) { if(p->next[str[i]-'0']==NULL)/*如果以前没有建立过该结点*/ { if(p->flag==false) { p->next[str[i]-'0']=newset();/*建立新结点*/ p=p->next[str[i]-'0']; if(i==len-1)/*只有当指向到数字串末尾时,标记结点为true;*/ { p->flag=true; } else { p->flag=false; } } else/*否则说明当前建立的数字串覆盖了以前的数字串(当前更长),不能拨打号码*/ { is_phone=false; return ; } } else { if(i==len-1&&p->flag==false)/*如果当前号码是以前出现过的号码的前缀(当前更短)*/ { is_phone=false; return ; } p=p->next[str[i]-'0']; } } return ; } int del(node *t)/*释放内存*/ { if(t==NULL) return 0; for(int i=0;i<10;i++) { if(t->next[i]!=NULL) { del(t->next[i]); } } free(t); return 0; } int main() { int ncase,n; char str[1001]; scanf("%d", &ncase); while(ncase--) { root=newset(); scanf("%d",&n); is_phone=true; for(int i=0;i<n;i++) { scanf("%s",str); insert(str); } if(is_phone) printf("YES\n"); else printf("NO\n"); del(root); } return 0; }
HDU1671
拨电话号码,
2391197625999911254265113123401234401234598346输出:
NO
YES
用字典树先保存所有数据,那么据题意判断是否产生干扰就看每个节点的num值与它的所有存在的子节点的num值之和是否相等,如果不相等那么必然有干扰了。
如果字符串Xn=X1X2....Xn是字符串Ym=Y1Y2....Ym的前缀,有在插入的时候有两种情况:Xn在Yn之前插入,Xn在Yn之后插入。
(1)如果Xn在Yn之前插入,那么在插入Yn的时候必然经过Xn的路径,此时可以根据判断在这条路径上是否已经有结点被标记已经构成完成的字符串序列来判断是否存在Yn的前缀;
(2)如果Xn在Yn之后插入,那么插入完成之后当前指针指向的结点的next数组中的元素必定不全为NULL。 0 0
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树。。
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- [添加用户]解决useradd 用户后没有添加用户Home目录的情况,Linux改变文件或目录的访问权限命令,linux修改用户密码,usermod的ysuum安装包。飞
- Automation作业调度问题:Only one instance of etlslave.pl allow to run
- 二分图小结
- 【算法导论实验5】贪心-活动安排问题与背包问题
- DotNetBar for Windows Forms用户界面
- 字典树
- Linux命令-学习shell脚本之前的基础知识
- 自学C++之自定义数据类型与自定义类之数组使用4例
- 深度学习: 推进人工智能的梦想
- c++继承经典例子
- 解析nginx负载均衡
- Oracle错误表
- LeetCode OJ - LRU Cache
- Informatica作业问题:日志一直显示为Running