Phone List

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

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
 
只有一个地方值得注意,就是我代码中标记的那个地方
package 字典树;

import java.util.Scanner;

class Trie {
private Node root; public Trie() {
root = new Node();
} public boolean insert(String str) {
Node t = root;
for (int i = 0; i < str.length(); i++) {
if (t.nodes[str.charAt(i) - '0'] == null) {
Node node = new Node();
t.nodes[str.charAt(i) - '0'] = node;
} t = t.nodes[str.charAt(i) - '0'];
if (t.isNumber) { //如果相同前缀出现过了
return false;
}
}
t.isNumber = true;
//写了这段就AC了,判断一下他的子节点是否存在,存在就证明它绝对是某个的前缀 ,比如说输入 91111 911 如果不加此判断 91111的标记是在最后一个
// ‘1’,再次输入 911 就判断不到了 。。 WA了好多次
for(int i=0;i<=9;i++){
if(t.nodes[i]!=null) return false;
}
return true;
} class Node {
boolean isNumber;
Node[] nodes; public Node() {
isNumber = false;
nodes = new Node[11];
}
}
} public class hdu_1671 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tcase = sc.nextInt();
while (tcase-- > 0) {
boolean flag = true;
Trie tree = new Trie();
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
String str = sc.next();
if (flag) {
flag = tree.insert(str);
}
}
if (!flag)
System.out.println("NO");
else
System.out.println("YES");
}
}
}

hdu 1671(字典树判断前缀)的更多相关文章

  1. Phone List HDU - 1671 字典树

    题意:给出一堆一组一组的数字  判断有没有哪一个是另外一个的前缀 思路:字典树 插入的同时进行判断  不过 当处理一组数字的时候 需要考虑的有两点1.是否包含了其他的序列2.是否被其他序列包含 刚开始 ...

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

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

  3. HDU 1251 统计难题(字典树计算前缀数量)

    字典树应用,每个节点上对应的cnt是以它为前缀的单词的数量 #include<stdio.h> #include<string.h> struct trie { int cnt ...

  4. Trie(字典树,前缀树)_模板

    Trie Trie,又经常叫前缀树,字典树等等. Trie,又称前缀树或字典树,用于保存关联数组,其中的键通常是字符串.与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定.一个节点的 ...

  5. HDU 5687 字典树插入查找删除

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...

  6. HDU 5384 字典树、AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...

  7. Repository HDU - 2846 字典树

    题意:给出很多很多很多很多个 单词 类似搜索引擎一下 输入一个单词 判断有一个字符串包含这个单词 思路:字典树变体,把每个单词的后缀都扔字典树里面,这里要注意dd是一个单词 但是把d 和dd都放字典树 ...

  8. 字典树(前缀树)-Java实现

    字典树 字典树是一种树形结构,优点是利用字符串的公共前缀来节约存储空间.在这提供一个自己写的Java实现,非常简洁. 根节点没有字符路径.除根节点外,每一个节点都被一个字符路径找到. 从根节点到某一节 ...

  9. HDU 5687 字典树入门

    Problem C Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

随机推荐

  1. H5背景音乐自动播放(兼容微信IOS,进程后台切换自动停止播放,本文例子为Vue写法)

    <template> <audio src="./static/music.mp3" id="bgMusic" preload="a ...

  2. JavaScript SandBox沙箱设计模式

    沙箱模式常见于YUI3 core,它是一种采用同一构造器(Constructor)生成彼此独立且互不干扰(self-contained)的实例对象,而从避免污染全局对象的方法. 命名空间 JavaSc ...

  3. linux配置虚拟机网络环境(老师要求的host-only)

    我这个人就是懒,这TMD是全天下最坑爹的缺点了,当然爆粗口也是缺点,让我发泄一下吧.T^T 从n久之前,开了hadoop课的一天,我就想着要配置好,结果两次课连眼镜都忘了带,可想而知是什么陪我度过了那 ...

  4. 解决 cmd dos 下 无法显示中文

    在做程序开发的时候经常需要在使用命令行进行操作, dos环境本身是不支持中文的,有时候中文编码的问题就像苍蝇一样讨厌,下面提供几种常用的手段解决win7环境下中文显示乱码的问题: 方法一: 修改注册表 ...

  5. POJ2155 树状数组

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 26650   Accepted: 9825 Descripti ...

  6. Bigbluebutton安装过程

    BigBlueButton安装过程(翻译) 欢迎来到BigBlueButton 1.0-beta安装指南(以下简称BigBlueButton 1.0).BigBlueButton是一个开放源代码的网络 ...

  7. JavaMail实现邮件的发送

    1,拷贝mail.jar 和activation.jar到项目中 2,开启邮箱的 POP3/SMTP服务,以QQ邮箱为例 进去QQ邮箱-->设置-->账号-->进行设置如下图 注意: ...

  8. ZooKeeper概述(三)

    ZooKeeper:分布式应用的分布协调服务 ZooKeeper是一个为分布式应用提供的分布的开源的协调服务.它暴露一组简单的原子操作,分布式系统可以在这之上为同步,配置管理,和组和命名实现更高级的服 ...

  9. 任务调度 Quartz 学习(一) SimpleTrigger

    概述: 在实际开发过程中,会遇到很多任务调度的需求. 比如说:某网站要在每周一上午9点更新网站数据,并发邮件通知用户: 再比如某论坛需求:每隔半个小时生成精华文章的RSS文件,每天凌晨统计论坛用户的积 ...

  10. Mybatis 参考

    1:Mybatis最入门---ResultMaps基本用法 2:Mybatis最入门---ResultMaps高级用法(上) 3:Mybatis最入门---ResultMaps高级用法(下) 4:My ...