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. 2-SAT入门

    大概学了一下2-SAT,写了一道模板和一道USACO 输出一个方案的话,tarjan缩点后倒着拓扑,染色输出. 求任何解下选哪个就得枚举每个点dfs来判断选哪个. HIT 1917(2-sat模板) ...

  2. 【P2602】【ZJOI2012】数字计数

    传送门 Description 给定两个正整数\(a\)和\(b\),求在\([a,b]\)中的所有整数中,每个数码(\(digit\))各出现了多少次. Input 两个正整数\(a,b\) Out ...

  3. mysql主从同步加读写分离

    首先主从同步,一旦建立,指定了用户,就不能更改了,否则会有错误.1063 Error 'Duplicate entry '%-test-' for key 'PRIMARY'' on query. D ...

  4. dubbox小demo

    概述: 我们建立两个web项目,一个是service负责提供服务,另一个是web项目负责调用服务. 两个项目都是 maven Project 项目 生产者项目: 项目中主要就是: pom文件,引入相关 ...

  5. MyEclipse安装FreeMarker插件

      MyEclipce8.6中安装FreeMarker插件,这绝对是最简单的方法.   步骤如下:   (一)打开http://sourceforge.net/projects/freemarker- ...

  6. 常州模拟赛d7t3 水管

    分析:第一问还是很好做的,关键是怎么做第二问.我们可以每次删掉最小生成树上的一条边,然后再求一次最小生成树,看边权和大小和原来的是不是一样的,不过这个做法效率很低. 考虑Kruskal算法的原理,每次 ...

  7. Spring框架介绍和原理

    SpringMVC框架介绍 1) Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面. Spring 框架提供了构建 Web 应用程序的全功 ...

  8. google 浏览器的调试模式

    1.电脑模式: 而在手机模式下的调试是:

  9. Codeforces Round #201 (Div. 2)C,E

    数论: C. Alice and Bob time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  10. C语言双链表遍历,插入,删除

    #include<stdio.h> #include<stdlib.h> #include <string.h> #define bzero(a, b) memse ...