hdu 1672Phone List&&poj 3630Phone List
2015-12-02 20:51
519 查看
Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15379 Accepted Submission(s): 5175
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
动态与静态差距:
动态空间代码(poj过不了):
#include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef struct TrieNode{ int data; TrieNode *next[10]; TrieNode(){ data=0; memset(next,0,sizeof(next)); } }; TrieNode *root=NULL; void Build(char *c){ TrieNode *p=root; TrieNode *q=NULL; int i,v,l=strlen(c); for(i=0;i<l;i++) { v=c[i]-'0'; if(p->next[v]==NULL){ q=new TrieNode; p->next[v]=q; } p=p->next[v]; p->data++; } } int Find(char *c){ int i,v,l=strlen(c); TrieNode *p=root; for(i=0;i<l;i++) { v=c[i]-'0'; if(p->next[v]==NULL){ return 0;break; } else p=p->next[v]; } return p->data; } void Delate(TrieNode *root){ for(int i=0;i<10;i++) if(root->next[i]) Delate(root->next[i]); delete(root); } int main() { int t,n,s,i,p; char c[10001][15]; cin>>t; while(t--) { s=0; root=new TrieNode; cin>>n; for(i=1;i<=n;i++) { scanf("%s",c[i]); Build(c[i]); } for(i=1;i<=n;i++) { p=Find(c[i]); if(p==0)break; else if(s<p)s=p; } if(s>1||p==0)printf("NO\n"); else printf("YES\n"); Delate(root); } return 0; }
静态空间代码:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int data_TrieNode; struct TrieNode{ int data; TrieNode *next[10]; void init(){ data=0; memset(next,0,sizeof(next)); } }Tree[100001]; inline TrieNode *new_TrieNode(){ Tree[data_TrieNode].init(); return &Tree[data_TrieNode++]; } TrieNode *root=NULL; void Build(char *c){ TrieNode *p=root; TrieNode *q=NULL; int i,v,l=strlen(c); for(i=0;i<l;i++) { v=c[i]-'0'; if(p->next[v]==NULL){ q=new_TrieNode(); p->next[v]=q; } p=p->next[v]; p->data++; } } int Find(char *c){ int i,v,l=strlen(c); TrieNode *p=root; for(i=0;i<l;i++) { v=c[i]-'0'; if(p->next[v]==NULL){ return 0;break; } else p=p->next[v]; } return p->data; } int main() { int t,n,s,i,p; char c[10001][15]; cin>>t; while(t--) { data_TrieNode=0; s=0; root=new_TrieNode(); cin>>n; for(i=1;i<=n;i++) { scanf("%s",c[i]); Build(c[i]); } for(i=1;i<=n;i++) { p=Find(c[i]); if(p==0)break; else if(s<p)s=p; } if(s>1||p==0)printf("NO\n"); else printf("YES\n"); } return 0; }
相关文章推荐
- 一行代码求解1-n的累加和(&实现)
- C语言基础知识1
- Perl Print Win32 Console Windows 控制台 print Unicode 问题
- JSP+servlet+JavaBean
- proxy模式
- php高版本安装ecshop错误解决方法
- 2015-12-02 定时自动执行存储过程
- js判断数据类型
- 浅谈CSRF攻击方式
- ‘python’不是内部或外部命令
- 通信系统传输模型仿真实现
- jQuery选择器之表单选择器
- 利用Matlab Image Processing Toolbox中的图像配准工具
- Java 没有MPI
- Object-C,NSArraySortTest,数组排序3种方式
- Object-C,NSArraySortTest,数组排序3种方式
- Object-C,NSArraySortTest,数组排序3种方式
- Spring容器注解详解<四>
- Profiling 性能分析
- 2015-12-02 计划任务维护数据库