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
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
 
题意:
一次给出n个电话号码,如果刚给出的一个电话号码的前几位包含前面给出的某一个号码则这个号码不能拨打(输入那前几位时就会马上打出去了)。
代码:
 //字典树的权值作为标记点,每输入一个号码,查找他每一位的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 字典树的更多相关文章

  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. Linux文件(区域)锁函数 -- open()、fcntl()

    一.什么是文件锁定 对于锁这个字,大家一定不会陌生,因为我们生活中就存在着大量的锁,它们各个方面发挥着它的作用,现在世界中的锁的功能都可归结为一句话,就是阻止某些人做某些事,例如,门锁就是阻止除了屋主 ...

  2. 修改mysql默认字符编码出现的Job failed to start解决方法

    5.5以后的版本对字符编码方式修改的办法,原来在[mysqld]下的修改已经发生了变化,正确方式如下: [mysqld]下添加的应该为: character-set-server=utf8 colla ...

  3. Postman 发送http请求工具

    http://donglegend.com/2016/10/28/Postman/ Postman 发现一款发送Web API & HTTP 请求的工具,没错,就是Postman.推荐给大家, ...

  4. 使用github pages, hexo搭建个人博客教程

    具体的原理性的东西就不说了直接上教程,怕等下自己忘了. 一. github 阶段 申请一个github 账号并成功登录进去. 创建一个名字为xxx.github.io的空项目. 二. hexo 阶段 ...

  5. PHP程序员进阶学习书籍参考指南

    PHP程序员进阶学习书籍参考指南 @heiyeluren lastmodify: 2016/2/18     [初阶](基础知识及入门)   01. <PHP与MySQL程序设计(第4版)> ...

  6. css3弹性盒模型

    一.简介 css3引入了新的盒模型——弹性盒模型,该模型决定一个盒子在其他盒子中的分布方式以及如何处理可用的空间.使用该模型,可以很轻松的创建自适应浏览器窗口的流动布局或自适应字体大小的弹性布局. 目 ...

  7. opendrive

    opendrive和其他许多网盘一样.注册拥有5G的免费空间.每天1G的免费外链流量.更重要的是,他能够给你提供一个直接外链!这是国内外许多网盘都没有的.当你上载了一个MP3,你想用直接外链的形式在博 ...

  8. Spring 定时器Quartz的用法

    Spring定时器Quartz的用法也很简单,需要引入quartz-all-1.5.2.jar java代码如下: package com.coalmine.desktop; import java. ...

  9. 这交互炸了:饿了么是怎么让Image变成详情页的

    这交互炸了:饿了么是怎么让Image变成详情页的 晚上叫外卖,打开饿了么,发现推了一个版本,更新以后,点开了个鸡腿,哇,交互炫炸了. 本文同步自wing的地方酒馆 不过还是有槽点.我是无意中才发现可以 ...

  10. 64位win7下安装SQL Server 2008(图文解说版)

    运行sql安装 单击安装-全新的sql server独立安装,如果我们准备好了故障转移群集,那么我们就可以创建故障转移群集sql 常规检查 一笑而过 选择版本,或者输入密钥自动识别版本 授权协议 支持 ...