poj 3630 静态字典树入门
来源:互联网 发布:java常见注解 编辑:程序博客网 时间:2024/06/06 20:29
点击打开链接
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:
- Emergency 911
- Alice 97 625 999
- 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题意:输入一系列电话号码数字,若有某个数字是另外一个数字的前缀就输出NO,否则YES。
思路:这个题思路清晰,很容易,但是有个坑就是动态建树会超时,因此这个题要用静态建树,空间换时间。
#include<stdio.h>#include<stdlib.h>#include<string.h>struct node{int cnt;int flag;struct node *next[10];void init(){cnt=flag=0;for(int i=0;i<10;i++)next[i]=NULL;}}a[100000];char s[12];int mark;int k=0;void insert(){int i,t,len=strlen(s);node *p=&a[0];for(i=0;i<len;i++){t=s[i]-'0';if(p->next[t]==NULL)p->next[t]=&a[++k];p=p->next[t];p->cnt++;if(p->flag>0)//如果这个节点是某个数字的尾数,标记 mark=1; }p->flag++;if(p->cnt>1)//如果这个节点之前已经是别的数字的节点,标记 mark=1;}void del(){for(int i=0;i<100000;i++)a[i].init();}int main(){int T;scanf("%d",&T);while(T--){int n,i;scanf("%d",&n);mark=k=0;for(i=0;i<n;i++){scanf("%s",&s);insert();}if(mark==1)printf("NO\n");elseprintf("YES\n");del();}}
- poj 3630 静态字典树入门
- POJ 3630 静态字典树
- POJ 3630 Phone List 静态字典树
- 【POJ 3630】Phone List(静态字典树)
- poj 3630 Phone List (字典树 +静态字典树)
- POJ 3630 && HDU 1671 Phone list(静态字典树)
- POJ 3630 Phone List 静态字典树Trietree
- 【POJ】3630 - Phone List(字典树(静态建树))
- POJ 3630:Phone List(字典树入门)
- POJ 3630 字典树
- 字典树入门——POJ 2001
- poj--2503+字典树入门题
- POJ 2503 Babelfish 字典树入门题
- POJ 2503:Babelfish(字典树入门)
- poj 2418 字典树+dfs 入门题
- poj 3630 Phone List trie树—字典树 静态数组版!!!
- poj 3630 / hdu 1671 Phone List 【Trie字典树 动态创建&静态创建】
- nyoj 163 Phone List && poj 3630 Phone List <字典树 动态建树&&静态建树>
- Hadoop MapReduce 简单案例--求素数个数
- vue 2 使用Bus.js进行兄弟(非父子)组件通信 简单案例
- PHP文件上传学习(多文件,ajax无刷新方式)
- 深度学习中Dropout和Layer Normalization技术的使用
- Android Streaming Live Camera Video to Web Page
- poj 3630 静态字典树入门
- MySQL索引实现类型
- C#--再识ref、out关键字
- C++注意点
- 数据库和数据仓库的区别
- 300.13从磁盘读取一个文件到内存中,再打印到控制台
- Object.finalize方法的实现原理
- 周末训练笔记+ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛+2017 ACM-ICPC 亚洲区(南宁赛区)网络赛(9.23,9.24)
- 并查集——小米笔试题求朋友圈个数,分小组个数