hdu1671 Phone List 字典树(小组赛E)
来源:互联网 发布:prim算法描述 编辑:程序博客网 时间:2024/04/30 23:21
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671
Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 18366 Accepted Submission(s): 6181
Problem Description
Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let’s say the phone catalogue listed these numbers:
1. Emergency 911
2. Alice 97 625 999
3. Bob 91 12 54 26
In this case, it’s not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob’s phone number. So this list would not be consistent.
1. Emergency 911
2. Alice 97 625 999
3. Bob 91 12 54 26
In this case, it’s not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob’s phone number. So this list would not be consistent.
Input
The first line of input gives a single integer, 1 <= t <= 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 <= n <= 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.
Output
For each test case, output “YES” if the list is consistent, or “NO” otherwise.
Sample Input
2391197625999911254265113123401234401234598346
Sample Output
NOYES
思路:字典树
基本的操作
1.建立:把母字符串每个字符串存入树,如存入ab、abc、df、de,如图
2.查找
3.插入
4.删除
(1) 从根结点开始一次搜索;
(2) 取得要查找关键词的第一个字母,并根据该字母选择对应的子树并转到该子树继续进行检索;
(3) 在相应的子树上,取得要查找关键词的第二个字母,并进一步选择对应的子树进行检索。
(4) 迭代过程……
(5) 在某个结点处,关键词的所有字母已被取出,则读取附在该结点上的信息,即完成查找。
其他操作类似处理
节点基本结构
建立
查找
代码:
#include<stdio.h>#include<stdlib.h>#include<string.h>typedef struct node{ node *next[10]; int v; int f;}*tree,t;tree root;int flag;void insert(char *str){ tree p=root,newnode; for(;*str!='\0';str++) { if(p->next[*str-'0']!=NULL) { p->f=1; p=p->next[*str-'0']; if(p->v==-1)//前面的串中出现过当前串的前缀 flag=1; } else { newnode=(tree)malloc(sizeof(t)); for(int i=0;i<=9;i++) newnode->next[i]=NULL; newnode->v=0; newnode->f=0; p->f=1; p->next[*str-'0']=newnode; p=newnode; } } p->v=-1; if(p->f)//当前串是之前出现过的串的前缀 { flag=1; }}int deal(node* T)//释放内存空间{ int i; for(i=0;i<=9;i++) { if(T->next[i]!=NULL) deal(T->next[i]); } free(T); return 0;}int main(){ int m,n,num; char str[15]; scanf("%d",&m); while(m--) { flag=0; root=(tree)malloc(sizeof(t)); for(int i=0;i<=9;i++) root->next[i]=NULL; root->v=0; root->f=0; scanf("%d",&n);getchar(); while(n--) { gets(str); if(flag)//算是优化吧,可是 还是很慢呀,差距,差距啊 continue; insert(str); //puts(str); } if(!flag) printf("YES\n"); else printf("NO\n"); deal(root); } return 0;}
0 0
- hdu1671 Phone List 字典树(小组赛E)
- 【字典树】 hdu1671 Phone List
- Phone List(hdu1671字典树)
- hdu1671 Phone List (字典树)
- HDU1671 Phone List【字典树】
- HDU1671 Phone List (字典树)
- hdu1671-Phone List 字典树
- HDU1671 Phone List 解题报告--字典树
- HDU1671 poj3630 Phone List 字典树
- HDU1671 Phone List(字典树)
- hdu1671 Phone List (字典树)
- hdu1671 Phone List (字典树)
- hdu1671 Phone List(字典树)
- HDU1671:Phone List(字典树)
- [复习][HDU1671]字典树(trie树)phone list
- hdu1671 Phone List(简单字典树,注意超空间)
- C++——字典树(Trie树)例题——Phone List(POJ3630)(HDU1671)
- HDU1671 Phone List(必须删除字典树,否则Memory Limit Exceeded)
- Java String 类型对象不能改变
- Ubuntu install ant
- cocos2dx原生lua绑定工具的总结
- C语言初学者代码
- 进制转换
- hdu1671 Phone List 字典树(小组赛E)
- ubuntu 下 ssldump 编译问题解决,及安装使用
- Hibernate之自定义ID生成器
- java: Exception Handle Mechanism
- java编程思想读书笔记 第十五章 泛型 (泛型方法)
- 记录一下python的数据结构 - dict
- Codeforces 732B-Cormen—The Best Friend Of a Man
- Hibernate:一对一双向主键关联
- Android APP--两个Activity传递数据