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. apache和IIS共享80端口解决办法

    第一步:把iis所发布的网站默认端口由80改为8080:第二步:修改apache的httpd.conf配置文件. 首先,要让apache支持转发也就是做iis的代理那么就要先启 用apache的代理模 ...

  2. httpClient需要的jar包

  3. 一个JAVA题引发的思考

    转载自:http://www.cnblogs.com/heshan664754022/archive/2013/03/24/2979495.html 十年半山 今天在论坛闲逛的时候发现了一个很有趣的题 ...

  4. 双向数据绑定实现之Object.defineProperty

    vue.js利用的是es5的 defineproperty 特性实现的双向数据绑定,了解一下基本原理. 举例 var person= {}; Object.defineProperty(person, ...

  5. ZooKeeper文档(二)

    ZooKeeper:因为协调的分布式系统是一个动物园 ZooKeeper对分布式应用来说是一个高性能的协调服务.它暴露通常的服务-比如命名,配置管理,同步,和组服务-用一种简单的接口,所以你不用从头开 ...

  6. How to Disable System Integrity Protection (rootless) in OS X El Capitan

    mac在10.11之后增加了一个功能,号称"System Integrity Protection, often called rootless",有了这个功能,以下目录的东西都不 ...

  7. 【C++ STL】容器的选择

    c++提供了各具特长的容器,那么我们该如何选择最佳的容器? 缺省状态下应该选择vector,因为vector内部结构最简单,并允许随机存取,所以数据的存取十分方便,数据的处理也快. 如果经常要在头部和 ...

  8. Sass 基本特性-基础 笔记

    一.变量声明 $ 变量的声明使用 $  所有的变量必须声明到变量调用之前 从3.4版本开始,Sass已经可以正确处理作用域的概念     在局部范围声明一个已经存在于全局内的变量时,局部变量就会成为全 ...

  9. 【bzoj1572-工作安排】贪心

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1572 题意: 约翰接到了N份工作,每份工作恰好占用一天的时间.约翰从第一天开始工作,他可以任意 ...

  10. js_微信分享,监听点击分享,分享成功,取消分享,分享失败回调

    2017-06-13 见代码: function weixinShare() { var links = links = "www.youku.com"; common.get_o ...