[ACM] hdu 1671 Phone List (字典树)

来源:互联网 发布:经济数据公布时间表 编辑:程序博客网 时间:2024/05/17 23:53

Phone List



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
2391197625999911254265113123401234401234598346
 

Sample Output
NOYES
 

Source
2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)



解题思路:

判断输入的串中是否存在某个串是另外串的前缀。
建立字典树,关键是设立某个串结尾的标志,即在哪个字母结尾。要判断是否存在前缀要考虑两种情况,一是当前输入的串是否是以前输入的串的前缀,而是以前输入的串是否是当前输入的串的前缀。前一种情况在查找该串时,会从第一个字母查找到最后一个字母,中间不返回任何值,说明当前的串是以前的前缀,后一种情况,当在查找当前串的时候遇到以前串结束的标志,则说明以前串是当前串的前缀。代码中结束的标志为保存串中最后一个字母的那个节点的cnt值为-1.

如图:


代码:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <malloc.h>  
  3. #include <algorithm>  
  4. #include <string.h>  
  5. #include <stdio.h>  
  6. using namespace std;  
  7. const int maxn=10;  
  8. bool ok;  
  9. char str[12];  
  10. int t,n;  
  11.   
  12. struct Trie  
  13. {  
  14.     int cnt;  
  15.     Trie *next[maxn];  
  16. };  
  17.   
  18. Trie *root;  
  19.   
  20. void CreateTrie(char *str)  
  21. {  
  22.     int len=strlen(str);  
  23.     Trie*p=root,*q;  
  24.     for(int i=0;i<len;i++)  
  25.     {  
  26.         int id = str[i]-'0';  
  27.         if(p->next[id] == NULL)  
  28.         {  
  29.             q = (Trie *)malloc(sizeof(Trie));  
  30.             q->cnt = 1;  
  31.             for(int j=0; j<maxn; ++j)  
  32.                 q->next[j] = NULL;  
  33.             p->next[id] = q;  
  34.             p = p->next[id];  
  35.         }  
  36.         else  
  37.         {  
  38.             p = p->next[id];  
  39.         }  
  40.     }  
  41.     p->cnt=-1;//串末尾的标志  
  42. }  
  43.   
  44. int findTrie(char *str)  
  45. {  
  46.     int len=strlen(str);  
  47.     Trie *p=root;  
  48.     for(int i=0;i<len;i++)  
  49.     {  
  50.         int id=str[i]-'0';  
  51.         if(p->next[id]==NULL)  
  52.             return 0;//没有建过树  
  53.         if(p->next[id]->cnt==-1)  
  54.             return -1;//以前串是当前串的前缀  
  55.         p=p->next[id];  
  56.     }  
  57.     return -1;//当前串是以前串的前缀  
  58. }  
  59.   
  60. void release(Trie *root)//释放空间  
  61. {  
  62.     for(int i=0;i<maxn;i++)  
  63.     {  
  64.         if(root->next[i])  
  65.             release(root->next[i]);  
  66.     }  
  67.     free(root);  
  68. }  
  69. int main()  
  70. {  
  71.     scanf("%d",&t);  
  72.     while(t--)  
  73.     {  
  74.         ok=1;  
  75.         root=(Trie*)malloc(sizeof(Trie));//root为指针类型,需要申请空间  
  76.         for(int i=0; i<10; ++i)  
  77.             root->next[i] = NULL;  
  78.         scanf("%d",&n);  
  79.         while(n--)  
  80.         {  
  81.             scanf("%s",str);  
  82.             if(findTrie(str)==-1)  
  83.                 ok=0;//有前缀  
  84.             if(!ok)  
  85.                 continue;//有前缀,后面的就不用建树了  
  86.             CreateTrie(str);  
  87.         }  
  88.         if(ok)  
  89.             printf("YES\n");  
  90.         else  
  91.             printf("NO\n");  
  92.         release(root);  
  93.     }  
  94.     return 0;  
  95. }  
0 0
原创粉丝点击