hdu Phone List
来源:互联网 发布:淘宝老版本苹果版下载 编辑:程序博客网 时间:2024/05/17 21:52
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--------------------------------------------------------------------------这个字典树,要加上一个释放空间的deal函数,否则会超内存#include<stdio.h>#include<string.h>#include<algorithm>#include<malloc.h>using namespace std;typedef struct tree{tree *next[10];int con;}Trie;Trie *root;void createTrie(char *str){Trie *p,*q;int len,i,j,id;q = root;len = strlen(str);for(i=0;i<len;i++){id = str[i] - '0';if(q->next[id]==NULL){p = (Trie*)malloc(sizeof(Trie));q->next[id] = p;q = q->next[id];q->con=1;for(j=0;j<10;j++)q->next[j]=NULL;}else{q = q->next[id];q->con++;}}//q->con=-1;}void deal(Trie *t)//释放内存{int i;if(t==NULL)return ;for(i=0;i<10;i++){if(t->next[i]!=NULL)//递归调用从叶节点开始释放deal(t->next[i]);}free(t);return ;}int find(char *str){int len,i,j,id;Trie *q;q = root;len = strlen(str);for(i=0;i<len;i++){id = str[i] - '0';if(q->next[id]==NULL)return 0;elseq = q->next[id];}return q->con;}int main(){char phone[10010][20];int t,n,k,flag,i;memset(phone,0,sizeof(phone));scanf("%d",&t);while(t--){root = (Trie*)malloc(sizeof(Trie));for(i=0;i<10;i++)root->next[i]=NULL;scanf("%d",&n);for(i=0;i<n;i++){scanf("%s",phone[i]);createTrie(phone[i]);}flag=1;for(i=0;i<n;i++){k = find(phone[i]);if(k!=1){flag=0;break;}}if(flag)printf("YES\n");elseprintf("NO\n");memset(phone,0,sizeof(phone));deal(root);}return 0;}
0 0
- Phone List 1671hdu
- Hdu-1671 Phone List
- hdu 1671 Phone List
- HDU-1671-Phone List
- hdu 1671 Phone list
- hdu 1671 Phone List
- hdu 1671 Phone List
- HDU 1671 Phone List
- HDU 1671 Phone List
- HDU 1671 Phone List
- hdu 1671 Phone List
- hdu 1671 Phone List
- HDU-1671-Phone List
- Phone List hdu 1671
- hdu 1671 Phone List
- hdu 4287( PHONE LIST)
- hdu 1671 Phone List
- HDU 1671 Phone List(
- BZOJ 3122([Sdoi2013]随机数生成器-同余方程+负数逆元)
- UVA 11605 - Lights inside a 3d Grid(概率+数学)
- 南阳 oJ 585 尼姆博弈
- 关于oracle db 11gR2版本上的_external_scn_rejection_threshold_hours参数和scn headroom补丁问题
- Codeforces 196 D. The Next Good String
- hdu Phone List
- 存代码
- 鼠标放div,box-shadow加深;hover实例
- 一个Python程序员的进化
- NYOJ 题目105 九的余数(数学)
- 学习OpenCV的学习笔记系列(一)VS2010+OpenCV2.4.9环境配置
- static_cast和reinterpret_cast
- tnsnames.ora配置小结
- BZOJ 2241([SDOI2011]打地鼠-二分判断+贪心)