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
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.
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.
//字典树的权值作为标记点,每输入一个号码,查找他每一位的val值有没有等于1的,如果没有就把他放入字典树他的最后一
//位的val值置为1,如果有说明与前面的某一个冲突。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAX=;
const int CON=;
int nod[MAX][CON],val[MAX];
int sz;
void init()
{
sz=;
memset(nod[],,sizeof(nod[]));
val[]=;
}
void insert(char s[])
{
int len=strlen(s);
int rt=;
for(int i=;i<len;i++)
{
int id=s[i]-'';
if(nod[rt][id]==)
{
memset(nod[sz],,sizeof(nod[sz]));
nod[rt][id]=sz;
val[sz++]=;
}
rt=nod[rt][id];
}
val[rt]=;
}
int search(char s[])
{
int len=strlen(s);
int rt=,cnt=;
for(int i=;i<len;i++)
{
int id=s[i]-'';
if(nod[rt][id]==)
return ;
rt=nod[rt][id];
if(val[rt]==)
return ;
}
}
int main()
{
char ch[];
int t,n,flag;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d",&n);
flag=;
for(int i=;i<=n;i++)
{
scanf("%s",ch);
if(flag) continue;
flag=search(ch);
insert(ch);
}
if(flag) printf("NO\n");
else printf("YES\n");
}
return ;
}
HDU1671 字典树的更多相关文章
- hdu1671字典树
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu1671 字典树记录前缀出现次数
题意: 给你一堆电话号,问你这些电话号后面有没有相互冲突的,冲突的条件是当前这个电话号是另一个电话号的前缀,比如有 123456789 123,那么这两个电话号就冲突了,直接输出NO. 思 ...
- Phone List HDU1671 字典树Trie
模板题...不过会爆内存,要小心 #include <iostream> #include <cstdio> #include <string.h> #pragma ...
- 3道入门字典树例题,以及模板【HDU1251/HDU1305/HDU1671】
HDU1251:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题目大意:求得以该字符串为前缀的数目,注意输入格式就行了. #include<std ...
- POJ3630/HDU-1671 Phone List,字典树静态建树!
Phone List POJ动态建树TLE了~~~ 题意:拨打某个电话时可能会因为和其他电话号码的前几位重复而导致错误,现在给出一张电话单,求是否有某个电话是其他电话的前缀.是则输出NO,否则输出YE ...
- hdu1671 Phone List [字典树 hash]
传送门 Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hihoCoder 1014trie树(字典树)
hihoCoder 1014 题目提示已经很清楚了~ 贴代码…… #include <iostream> #include <cstdio> #include <cstr ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
随机推荐
- Android基础篇(一)
Android体系结构介绍 Android是一个移动开发平台,层次结构:操作系统(OS).中间件(Middle Ware).应用程序(Application) 具体: 操作系统(OS)-->各种 ...
- 原生JavaScript技巧
时常在技术论坛有看见一些比较好的示例,于是就出于一种收集并学习的态度,于是就保留下来啦~ 当然现在展示的也只是一部分,先放一部分出来尝尝鲜~~~
- Windows 7个性化配置,关闭Win7动画效果,设置窗口背景为“ 豆绿色”
减少眼睛疲劳配色(豆绿色): RGB:, , ,颜色名称:#C7EDCC 1.任务栏设置 2.关闭Win7动画效果 控制面板 -> 轻松访问 -> 优化视频显示 3.去掉窗口阴影 右键单击 ...
- 【Go入门教程3】流程(if、goto、for、switch)和函数(多个返回值、变参、传值与传指针、defer、函数作为值/类型、Panic和Recover、main函数和init函数、import)
这小节我们要介绍Go里面的流程控制以及函数操作. 流程控制 流程控制在编程语言中是最伟大的发明了,因为有了它,你可以通过很简单的流程描述来表达很复杂的逻辑.Go中流程控制分三大类:条件判断,循环控制和 ...
- Linux常用系统管理命令(top、free、kill、df)
top -c #任务管理器 free -m #查看内存使用情况 kill -9 2312 (说明:强制杀死进程 kill -9 pid ) df -h #查看磁盘 ...
- JavaScript创建对象及对象继承
面向对象的语言有一个标志,那就是他们都有类的概念,而通过类可以创建任意多个具有相同属性和方法的对象.但是在ECMAScript中没有类的概念,因此它的对象也与基于类的对象有所不同.实际上,JavaSc ...
- js string 转 int Number()
var numString = '122'; var numberInt = Number(numString); var res = numberInt/2; 结果: res = 61
- Python基本数据类型
一.整数 int(整形) 在32位机器上,整数的位数为32位,取值范围为-2**31~2**1-1,即-2147483648-2147483647 在64位系统上,整数的位数为64位,取值范围为-2* ...
- python 3.x urllib学习
urllib.request import urllib.request as ur url='http://ie.icoa.cn' user_agent = 'Mozilla/4.0 (compat ...
- ViewState与Session [转]
昨天偶然看到网上有人讨论究竟是该用viewstate还是session来保存信息. 忽然觉得有必要去深入的研究一下这两个东东了,我们先来看深入分析一下viewstate, 为了分析的相对完整性,先从简 ...