HDU 1671.Phone List【字典树】【5月15】
2016-05-15 14:43
471 查看
Phone ListTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16828 Accepted Submission(s): 5645 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. 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 2 3 911 97625999 91125426 5 113 12340 123440 12345 98346 Sample Output NO YES |
#include<iostream> #include<cstdio> #include<cstring> using namespace std; struct Node { int next[10]; int flag; }tree[100005]; int cnt; void Insert(char *s) { int p = 0; while(*s) { if(tree[p].next[*s-'0'] == -1) { tree[p].next[*s-'0'] = cnt; cnt ++; } p = tree[p].next[*s-'0']; s++; } tree[p].flag = 1; } bool Query(char *s) { int p = 0; while(*s) { if(tree[p].flag == 1) return true; else p = tree[p].next[*s-'0']; s++; } return false; } int T, n; bool key; char ss[10005][15]; int main() { scanf("%d", &T); while(T--) { for(int i = 0;i < 100005; ++i) { memset(tree[i].next, -1, sizeof(tree[i].next)); tree[i].flag = 0; } for(int i = 0;i < 10005; ++i) memset(ss[i], '\0', sizeof(ss[i])); scanf("%d", &n); cnt = 1; key = true; for(int i = 0;i < n; ++i) { scanf("%s", ss[i]); Insert(ss[i]); } for(int i = 0;i < n; ++i) { if(Query(ss[i])) { key = false; break; } } if(key) cout <<"YES"<< endl; else cout <<"NO"<< endl; } return 0; }
相关文章推荐
- 最大子数组和(The Maximum Subarray)
- 关于求最长递增子序列会超时的问题(使用栈)
- 使用nodejs的http模块创建web服务器
- 圣杯布局总结
- 人月神话阅读笔记03
- 青烟鸿影一盏茶,孤灯小楼听夜雨
- 找数2--nkoj3679(百度面试题)
- http statusCode(状态码) 200、300、400、500序列
- Linux发行版 CentOS6.5 禁用防火墙步骤
- 多线程问题
- 虚拟机里面安装Openfiler 2.99
- 嵌套映射
- c++ 的 坑真多之头文件
- 列表页调取二级栏目
- Centos下kafka 单机配置部署详解
- 光纤
- 百度之星2016资格赛 1004
- svchost占用cpu
- visual studio 2010 Error: IntelliSense: identifier "DWORD" is undefined
- Java的项目结构