HDU-1671

Phone List

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19302    Accepted Submission(s): 6529

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

建立字典树,每插入一个号码则将号码所在路径的count++,最后看如果有的号码末尾节点的count值不为1,则说明重复走过该路径,该号码为某号码的前缀。

注意一定要释放内存, 否则会爆内存。

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 struct node{
 5     int count;
 6     node*next[10];
 7     node(){
 8         count=0;
 9         for(int i=0;i<10;i++){
10             next[i]=NULL;
11         }
12     }
13 };
14
15 node* root;
16 node* a[10010];
17 int k=0;
18
19 void insert(char str[15]){
20     int len=strlen(str);
21     node *p=root;
22     for(int i=0;i<len;i++){
23         if(p->next[str[i]-‘0‘]==NULL)
24         p->next[str[i]-‘0‘]=new node;
25         p=p->next[str[i]-‘0‘];
26         p->count++;
27     }
28     a[k++]=p;
29 }
30
31 int serch(int n){
32     for(int i=0;i<k;i++){
33         if(a[i]->count!=1)
34         return 1;
35     }
36     return 0;
37 }
38
39 void del(node *p){
40     if(p==0)
41     return ;
42     for(int i=0;i<10;i++){
43         del(p->next[i]);
44     }
45     delete p;//free(p);
46 }
47
48 int main(){
49     int t;
50     char str[15];
51     cin>>t;
52     while(t--){
53         root=new node;
54         int n;
55         k=0;
56         cin>>n;
57         for(int i=0;i<n;i++){
58             //gets(str);
59             scanf("%s",str);
60             insert(str);
61         }
62         if(serch(n))
63         cout<<"NO"<<endl;
64         else
65         cout<<"YES"<<endl;
66         del(root);
67     }
68     return 0;
69 }
时间: 2024-08-06 12:25:15

HDU-1671的相关文章

HDU 1671 Phone List(Trie的应用与内存释放)

Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18217    Accepted Submission(s): 6120 Problem Description Given a list of phone numbers, determine if it is consistent in the sense th

字典树 Trie (HDU 1671)

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

HDU 1671 Phone List(字符处理)

题目 用字典树可以过,可是我写的字典树一直各种错误,,, 所以,我用了别的更简便的方法.. //去你妹的一直有问题的字典树!!! ////字典树,树的根是空的 // ////#include<iostream> //#include<cstdio> ////#include<list> //#include<algorithm> //#include<cstring> ////#include<string> ////#include

HDU 1671 Phone List (Trie树 好题)

Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11721    Accepted Submission(s): 3982 Problem Description Given a list of phone numbers, determine if it is consistent in the sense th

(字典树)HDU - 1671 Phone List

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 题意:给很多电话号码,如果在拨号的时候,拨到一个存在的号码,就会直接打出去,以致以这个号码为前缀的所有其他比这个号码长的号码将不能拨出,问是不是所有的号码都能拨. 分析:可以直接建立字典树,节点中用boolean变量表示当前是否组成电话号码,一旦在遍历完某条号码之前,已经出现存在号码,则发现问题,返回false,否则true. 我使用了一个反过来的方法,即只统计前缀出现次数,遍历完某条号码,如

poj 3630 / hdu 1671 Phone List 【Trie字典树 动态创建&amp;静态创建】

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25160   Accepted: 7641 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 catalogu

HDU 1671 (字典树统计是否有前缀)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 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. Emergenc

字典树模板+HDU 1671 ( Phone List )(字典树)

字典树指针模板(数组模板暂时还没写): 1 #include<cstdio> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 const int MAX=26; 6 const int maxn=1e4+100; 7 int N; 8 9 struct Trie 10 { 11 Trie *next[MAX]; 12 int v;///v要灵活使用,具体情况具体分析 13 }; 14

hdu 1671&amp;&amp;poj 3630 Phone List 【字典树】

题目链接:http://acm.acmcoder.com/showproblem.php?pid=1671 题意:问是否存在一个串是另一个串的前缀. 解法:建字典树,插入的串的结尾设置标志位,如果以后访问到,则存在一个串是另一个串的前缀.注意释放内存,不然超内存:(太弱,释放内存调了好久... 代码: #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm>

hdu 1671 Phone List(给定n个电话号码串,问这n个电话号码串中是否存在某一串是其它串的前缀,如果存在输出NO,否则YES)

1.动态申请的内存用完以后要释放,否则超内存. 2.代码: #include<cstdio> #include<cstring> #include<stdlib.h> using namespace std; struct Node { int cnt; Node *next[10]; void init() { cnt=0; for(int i=0;i<10;i++) { next[i]=NULL; } } }; Node *p_root; int n; voi