Phone List hdu1671

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

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

题意:给出一些数字,判断这些数字中是否有数字是其他数字的前缀。

分析:字典树

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<iostream>
 4 #include<string>
 5
 6 using namespace std;
 7 #define maxn 1000900
 8
 9 int dic[maxn][15],val[maxn],flag,cnt;
10
11 void inst(string s)
12 {
13     int u=0,v,len;
14     len=s.length();
15     for(int i=0;i<len;i++)
16     {
17         v=s[i]-‘0‘;
18         if(!dic[u][v])
19         {
20             dic[u][v]=++cnt;// 构造链表
21             memset(dic[cnt],0,sizeof(dic[cnt]));//优化
22         }
23         u=dic[u][v]; //u节点与v节点相连 dic的值指向下一个节点
24       if(i==len-1) val[u]++;// 标记该字符串结束位置
25     }
26 }
27
28 void get(string s)
29 {
30     int u=0,v,len;
31     len=s.length();
32     for(int i=0;i<len-1;i++)
33     {
34         v=s[i]-‘0‘;
35         if(val[dic[u][v]]) //如果该字符串结束之前发现有其他字符串结束点 说明产生前缀
36         {
37             flag=0;
38             return ;
39         }
40         u=dic[u][v];
41     }
42 }
43
44 int main()
45 {
46     int T;
47     scanf("%d",&T);
48     while(T--)
49     {
50         int n;
51         string s[100000];
52         scanf("%d",&n);
53         memset(dic[0],0,sizeof(dic[0]));
54         memset(val,0,sizeof(val));
55         cnt=0;
56         flag=1;
57         for(int i=0;i<n;i++)
58         {
59             cin>>s[i];
60             if(flag) inst(s[i]); //  这里以及下面最好都优化一下 不然会T
61             if(flag) get(s[i]);
62         }
63         for(int i=0;i<n&&flag;i++)
64             if(flag) get(s[i]);
65         else break;
66         if(flag) printf("YES\n");
67         else printf("NO\n");
68     }
69 }
时间: 2024-08-11 05:44:14

Phone List hdu1671的相关文章

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 tha

HDU1671 字典树

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

HDU1671 Phone List (字典树)

题目大意: 输入多串数字串,要求判断是否有的数字串是其它串的前缀.如果存在输出NO,否则输出YES. 解题思路: 用trie建立字典树,然后在每个数字串的结尾处标志1,之后每输入一个串,就判断一下.是否有之前的标志记号. #include<iostream> #include<malloc.h> #include<cstring> #include<cstdio> using namespace std; const int MAX_LEN = 11; ty

HDU1671——前缀树的一点感触

题目http://acm.hdu.edu.cn/showproblem.php?pid=1671 题目本身不难,一棵前缀树OK,但是前两次提交都没有成功. 第一次Memory Limit Exceeded: 前缀树是很费空间的数据结构,每个节点存放了字母(数字)个数个指针,正所谓用空间来换取时间. 我发现我忘记写析构函数了,所以测例多起来还不释放很容易超空间. 树形结构的析构也很有趣: 1 ~Node() 2 { 3 for (int i = 0; i < 10; ++i) 4 { 5 if (

hdu1671 Phone List [字典树 hash]

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

hdu----(1671)Phone List(Trie带标签)

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

HDU1671 Phone List【字典树】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1671 题目大意: 给你N个字符串,判断这N个字符串中是否存在一个字符串是另一个字符串的前缀,如果存在就 输出"NO",否则输出"YES". 思路: 建立一个字典树,将N个字符串存入字典树中,统计前缀出现次数.再查找这N个字符串,如果 出现字符串出现次数>1,则说明重复出现了两次,就输出"NO".如果都每出现,则输出"YES"

hihoCoder 1014trie树(字典树)

hihoCoder 1014 题目提示已经很清楚了~ 贴代码…… #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 100000 + 10; const int alNum = 26; struct Node{ int cnt; int next[alNum]; void init(){ memset(next,-1,sizeof(

Trie树入门及训练

什么叫Trie树? Trie树即字典树. 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希表高. (以上来自百度百科,点这里) 在我看来,Trie树是一棵26叉树(如果是统计字母的话),典型的空间换时间.那到底我们利用Trie来做什么呢? 1.统计单词 2.匹配前缀 千篇一律地需要提到