hdu 1671&& poj 3630 (trie 树应用)

 

Phone List
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 25280   Accepted: 7678

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:

  • Emergency 911
  • Alice 97 625 999
  • 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

Source

为何感觉一用指针就各种力不从心?

题意:t组实例,每组n个号码,判断里面号码是否有谁是谁的前缀,如果有则输出”NO“,否则“YES”;

亮点:插入新单词时给每个分支的结束处加一个结束标志,那么便出现了两种情况1.之前单词顺着这个分支走还没结束,而新插入的单词到这里结束了,说明新单词与旧单词重叠,则说明冲突,2.新插入单词未结束,而旧单词在这个节点结束了,说明旧单词是新单词的前缀,判断直接结束,冲突了~

 1 #include<iostream>
 2 #include<vector>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <math.h>
 7 #include<algorithm>
 8 #define ll long long
 9 #define eps 1e-8
10 using namespace std;
11 int  p;
12 struct nodes
13 {
14     int cnt;
15     struct nodes *next[11];
16 }* root,*temp,treenodes[100000];//静态申请节点比较省时间,也可以不用释放内存
17 
18 int inserts(char word[])
19 {
20     nodes *cur = root;
21     int i = 0;
22     while(word[i] )
23     {
24         int t = word[i] - '0';
25         if(cur->next[t] )
26         {
27             if(cur->next[t]->cnt == 1 ||  word[i + 1] == '\0')//这里的结束条件最容易出错,关键也在这里
28                 return 0;
29         }
30         else
31         {
32             temp = &treenodes[p++];
33             temp->cnt = 0;
34             for(int i = 0; i  < 10; i++)
35             {
36                 temp->next[i] = NULL;
37             }
38             cur->next[t] = temp;
39         }
40         cur = cur->next[t];
41         //printf("%d %c\n",cur->cnt,word[i]);
42         i++;
43     }
44     cur->cnt = 1;
45     //printf("%d %c\n",cur->cnt,*word);
46     return 1;
47 }
48 
49 int main(void)
50 {
51     int t,n,ans,last;
52     char phonenum[15];
53     scanf("%d",&t);
54     while(t--)
55     {
56         p = 0;
57         scanf("%d",&n);
58         root = &treenodes[p++];
59         root->cnt = 0;
60         for(int i = 0; i  < 10; i++)
61         {
62             root->next[i] = NULL;
63         }
64         last = 1;
65         for(int i = 0; i < n; i++)
66         {
67             scanf("%s",phonenum);
68             if(last)//如果只前的电话号码已经冲突,則不再插入新节点~
69                 ans = inserts(phonenum);
70             if(ans == 0)
71                 last = 0;
72         }
73         if(last)
74             printf("YES\n");
75         else
76             printf("NO\n");
77     }
78     return 0;
79 }

 

posted on 2015-08-12 14:58  鱼泪儿  阅读(191)  评论(0编辑  收藏  举报

导航