Phone List

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

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

字典树递归算法:

 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <map>
using namespace std;
#define ll long long int
#define INF 5100000
typedef struct node
{
struct node *next[];
int n;
bool end;
} trie;
trie *inti()
{
trie *t;
t=(trie *)malloc(sizeof(trie));
t->end=;
t->n=;
for(int i=; i<; i++) t->next[i]=NULL;
return t;
}
int insert(trie *t,char a[])
{ if(t->end)return ;
if(*a=='\0')
{
if(t->n)return ;
t->end=;
return ;
}
t->n++;
if(!t->next[*a-''])t->next[*a-'']=inti();
return insert(t->next[*a-''],a+);
}
void del(trie* t)
{
int i;
for(i=; i<; ++i)
{
if(t->next[i] != NULL)
{
del(t->next[i]);
}
}
free(t);
}
int main()
{
int n,m;
int i,j;
cin>>n;
char a[];
for(i=; i<n; i++)
{
trie *t;
t=inti();
cin>>m;
int fla=;
for(j=; j<m; j++)
{
scanf("%s",a);
if(!fla&&!insert(t,a))
fla=;
}
if(!fla)
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
del(t);
}
}

hdu1671字典树的更多相关文章

  1. HDU1671 字典树

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. hdu1671 字典树记录前缀出现次数

    题意:       给你一堆电话号,问你这些电话号后面有没有相互冲突的,冲突的条件是当前这个电话号是另一个电话号的前缀,比如有 123456789 123,那么这两个电话号就冲突了,直接输出NO. 思 ...

  3. Phone List HDU1671 字典树Trie

    模板题...不过会爆内存,要小心 #include <iostream> #include <cstdio> #include <string.h> #pragma ...

  4. 3道入门字典树例题,以及模板【HDU1251/HDU1305/HDU1671】

    HDU1251:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题目大意:求得以该字符串为前缀的数目,注意输入格式就行了. #include<std ...

  5. POJ3630/HDU-1671 Phone List,字典树静态建树!

    Phone List POJ动态建树TLE了~~~ 题意:拨打某个电话时可能会因为和其他电话号码的前几位重复而导致错误,现在给出一张电话单,求是否有某个电话是其他电话的前缀.是则输出NO,否则输出YE ...

  6. hdu1671 Phone List [字典树 hash]

    传送门 Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. hihoCoder 1014trie树(字典树)

    hihoCoder 1014 题目提示已经很清楚了~ 贴代码…… #include <iostream> #include <cstdio> #include <cstr ...

  8. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  9. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

随机推荐

  1. jmeter系列------变量的提取

    为了保证脚本的移植性,需要把一些变量提取出来,变量主要分为2类 1.一类是公用变量(全局变量),包括:IP.port.附件路径.CSV路径等,通常是使用用户定义的变量 组件 2.另一类是测试变量一般作 ...

  2. JS判断当前使用设备是pc端还是web端(转MirageFireFox)

    js判断当前设备 最近用bootstrap做自适应,发现仍然很难很好的兼容web端和PC端的现实. 仔细观察百度,淘宝,京东等大型网站,发现这些网站都有对应不同客户端的子站. 站点 PC端url we ...

  3. ScrollView嵌套ListView只显示一行

    错误描述 ScrollView嵌套ListView中导致ListView高度计算不正确,只显示一行. 解决方法 重写ListView的onMeasure方法,代码如下. @Override publi ...

  4. 银河麒麟操作系统U盘手动挂载,出现乱码

    使用银河麒麟操作系统,U盘手动挂载,U盘中中文字符显示为乱码??      对于银河麒麟操作系统的这一问题,可能是因为字符集的原因,需要在mount后加参数: sudo mount –o iochar ...

  5. redis info 命令详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt119 Redis Info 命令以一种易于理解和阅读的格式,返回关于 Red ...

  6. 多线程编程学习四(Lock 的使用).

    一.前言 本文要介绍使用Java5中 Lock 对象,同样也能实现同步的效果,而且在使用上更加方便.灵活,主要包括 ReentrantLock 类的使用和ReentrantReadWriteLock ...

  7. 【Java IO流】字节流和字符流详解

    字节流和字符流 对于文件必然有读和写的操作,读和写就对应了输入和输出流,流又分成字节和字符流. 1.从对文件的操作来讲,有读和写的操作——也就是输入和输出. 2.从流的流向来讲,有输入和输出之分. 3 ...

  8. 复杂关联SQL的优化

    昨天处理了一则复杂关联SQL的优化,这类SQL的优化往往考虑以下四点: 第一.查询所返回的结果集,通常查询返回的结果集很少,是有信心进行优化的: 第二.驱动表的选择至关重要,通过查看执行计划,可以看到 ...

  9. 团队作业4——第一次项目冲刺(Alpha版本)2017.4.23

    1.当天站立式会议照片 本次会议为第一次会议 本次会议在5号公寓1楼召开,本次会议内容: ①:做第一天的简单分工 ②:讨论每个人是否对安排的任务有苦难 ③:规定完成时间是在第二天之前 ④:遇到困难,及 ...

  10. 201521123078《Java程序设计》第2周学习总结

    1. 本周学习总结 **学会使用码云管理代码,包括将本地的代码上传至码云,和将码云上的项目保存至本地.编程要掌握重要的类名的使用,提高编程效率,避免想无头苍蝇一样** 2. 书面作业 使用Eclips ...