poj Phone List 3630 (字典树 判断前缀)
2015-10-17 11:18
323 查看
Phone List
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
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 25650 | Accepted: 7770 |
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
2 3 911 97625999 91125426 5 113 12340 123440 12345 98346
Sample Output
NO YES
//题意:
给n个字符串,判断这些字符串是否存在一个字符串是另一个字符串的前缀。若存在输出NO,否则输出YES。
#include<stdio.h> #include<string.h> #include<algorithm> #define N 100100 using namespace std; char s[10100][10]; int ch [10]; int word ; int sz; int init() { sz=1; memset(ch[0],0,sizeof(ch[0])); memset(word,0,sizeof(word)); } void insert(char s[]) { int i,j,l=strlen(s); int k=0; for(i=0;i<l;i++) { j=s[i]-'0'; if(!ch[k][j]) { memset(ch[sz],0,sizeof(ch[sz])); ch[k][j]=sz++; } k=ch[k][j]; word[k]++; } } int find(char s[]) { int i,j,l=strlen(s); int k=0; for(i=0;i<l;i++) { j=s[i]-'0'; k=ch[k][j]; if(word[k]==1) return 1; } return 0; } int main() { init(); int t,n,i; int flag; scanf("%d",&t); while(t--) { scanf("%d",&n); init(); for(i=0;i<n;i++) { scanf("%s",s[i]); insert(s[i]); } flag=1; for(i=0;i<n;i++) { if(!find(s[i])) { flag=0; break; } } if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- 【Android】图片切换组件ImageSwitcher的运用
- JVM的组成部分与内存管理
- nginx 配置多个二级域名
- 根据日期计算年龄
- 完整删除pod,解决某些无法解决的问题
- Android 基于ImageSwitcher实现的左右切换图片
- 通用异常处理框架
- No Entertainment!
- 【开源】OSharp3.3框架解说系列:开发计划与进度
- linux下的find文件查找命令与grep文件内容查找命令
- MySQL数据库InnoDB存储引擎中的锁机制--转载
- pkg文件--一种简单的游戏资源打包格式
- (一)如何成为wine开发者
- 接口interface的应用和工厂方法模式
- c++构造函数中发生错误,如何处理? http://www.opengpu.org/forum.php?mod=viewthread&tid=2918
- 常用性能相关工具
- 小学奥数 我家的门牌号
- Android得到本地SHA1、MD5(百度地图及Google地图)
- 8 Model binding 模型绑定(下)(Nancy 官方文档翻译)
- python 爬虫小记