hdu1671字典树入门题
来源:互联网 发布:剑三喵姐冷艳捏脸数据 编辑:程序博客网 时间:2024/04/30 00:13
Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7110 Accepted Submission(s): 2441
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.build建树2.search查找3.delete删除空间代码:#include<iostream>#include<vector>#include<string>#include<queue>#include<cstdio>#include<cstring>#define maxn 2005#define INF 0xfffffffusing namespace std;char s[10005][20];struct trie{ trie* next[11]; int count; trie(){ memset(next,0,sizeof(next)); count=0; }};trie* root=0;void build(char *a){ int l=strlen(a); trie* p=root; for(int i=0;i<l;i++) { int t=a[i]-'0'; if(p->next[t]==0) { p->next[t]=new trie(); } p=p->next[t]; p->count++; }}bool search(char *a){ int l=strlen(a); trie* p=root; for(int i=0;i<l;i++) { int t=a[i]-'0'; p=p->next[t]; } return p->count>1;}void deletetrie(trie* p){ for(int i=0;i<10;i++) { if(p->next[i]!=0) { deletetrie(p->next[i]); } } delete p;}int main(){ int t,n; scanf("%d",&t); while(t--) { scanf("%d",&n); root=new trie(); for(int i=1;i<=n;i++) { scanf("%s",s[i]); build(s[i]); } int flag=0; for(int i=1;i<=n;i++) { if(search(s[i])) { flag=true; break; } } if(flag) printf("NO\n"); else printf("YES\n"); deletetrie(root); }return 0;}静态数组的写法:主要是把节点都定义在一个数组里面,0表示根节点,每次都得把所有的节点都初始化为0,因为以前的结点的状态会影响现在的状态。
代码:#include<iostream>#include<vector>#include<string>#include<queue>#include<cstdio>#include<cstring>#define maxn 2005#define INF 0xfffffffusing namespace std;char s[10005][20];struct trie{ int next[11]; int count; trie(){ memset(next,0,sizeof(next)); count=0; }}T[100005];int num=0;void build(char *a){ int l=strlen(a); int p=0; for(int i=0;i<l;i++) { int t=a[i]-'0'; if(T[p].next[t]==0) { T[p].next[t]=++num; } p=T[p].next[t]; T[p].count++; }}bool search(char *a){ int l=strlen(a); int p=0; for(int i=0;i<l;i++) { int t=a[i]-'0'; p=T[p].next[t]; } return T[p].count>1;}int main(){ int t,n; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=0;i<=100005;i++) { memset(T[i].next,0,sizeof(T[i].next)); T[i].count=0; } num=0; for(int i=1;i<=n;i++) { scanf("%s",s[i]); build(s[i]); } int flag=0; for(int i=1;i<=n;i++) { if(search(s[i])) { flag=true; break; } } if(flag) printf("NO\n"); else printf("YES\n"); }return 0;}
- hdu1671字典树入门题
- hdu1671 字典树 入门
- HDU1671(字典树模板题)
- HDU1671字典树
- hdu1671之字典树
- hdu1671( 字典树)
- 字典树HDU1671
- hdu1671 字典树
- 字典树(hdu1671)
- HDU1671 字典树
- HDU1671(字典树)
- hdu1671 字典树
- HDU1671 字典树
- 【字典树】 hdu1671 Phone List
- hdu1671字典树+动态分配内存
- Phone List(hdu1671字典树)
- hdu1671 Phone List (字典树)
- hdu1671 PhoneList (字典树)
- shell笔记(五)——函数
- 一个音效播放类SoundManager
- eclipse 快捷运用
- 数据库2008登不上" 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误"
- C++嵌套类和局部类
- hdu1671字典树入门题
- UVa340 Master-Mind Hints
- OK6410启动代码(3)
- 开始C/C++生涯
- OK6410启动代码(4)
- iOS extracts: Adding Color
- windows 安装 flash builder 开发flex 4 项目
- uva 1368 DNA Consensus String(检索)
- Html5问题备忘