杭电Phone List1671
2015-12-06 14:39
381 查看
Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15391 Accepted Submission(s): 5180
[align=left]Problem Description[/align]
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.
#include<iostream> #include<cstdio> #include<cstring> using namespace std; 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> using namespace std; char s[10001][11]; int cmp(const void* a,const void* b) { return strcmp((char*)a,(char*)b); } int main() { int T,i,n; scanf("%d",&T); while(T--) { scanf("%d",&n); for(i=0;i<n;i++) scanf("%s",s[i]); qsort(s,n,sizeof(s[0]),cmp); int flag=1; for(i=0;i<n-1;i++) if(strncmp(s[i],s[i+1],strlen(s[i]))==0) { flag=0; break; } if(flag==1)printf("YES\n"); else printf("NO\n"); } system("pause"); return 0; } #include<cstdio> #include<vector> #include<iostream> #include<algorithm> using namespace std; int main() { int T,n; scanf("%d",&T); char str[100]; while(T--) { scanf("%d",&n); //v.clear(); vector<string> v; int k=n; while(n--) { scanf("%s",str); v.push_back(string(str)); } sort(v.begin(),v.end()); int flag=0; for(int i=0;i<k-1;i++) { if(v[i+1].find(v[i])!=-1) {flag=1;printf("NO\n");break;} else continue; } if(!flag) printf("YES\n"); } return 0; } */
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, output “YES” if the list is consistent, or “NO” otherwise.
[align=left]Sample Input[/align]
2 3 911 97625999 91125426 5 113 12340 123440 12345 98346
[align=left]Sample Output[/align]
NO YES
相关文章推荐
- android横竖屏切换和布局的问题
- android横竖屏切换和布局的问题
- ListView 属性详解 参考1
- 20135234马启扬-——信息安全系统设计基础第十三周学习总结
- hdu 2546 饭卡(01背包)
- Android数据存储五种方式总结
- 切换jdk,但是运行java -version仍然不变的原因
- DB2搭建SQL复制环境
- New UI-为TextView设置超链接跳转的三个方法
- MyBatis的association示例以及association的两种形式
- Hidden String(深搜)
- forget mysql pw problem
- Apache和Tomcat的区别
- PAT 1007 素数对猜想
- 联合主键条件过滤查询,记录一点点
- 重学hadoop技术
- 欢迎使用CSDN-markdown编辑器
- [Canvas绘图] 第14节 “路”在何方
- 哈希表总结
- 浙江大学PAT_乙级_1047. 编程团体赛(20)