hdu 1671(字典树判断前缀)
Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16223 Accepted Submission(s): 5456
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.
3
911
97625999
91125426
5
113
12340
123440
12345
98346
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(字典树判断前缀)的更多相关文章
- Phone List HDU - 1671 字典树
题意:给出一堆一组一组的数字 判断有没有哪一个是另外一个的前缀 思路:字典树 插入的同时进行判断 不过 当处理一组数字的时候 需要考虑的有两点1.是否包含了其他的序列2.是否被其他序列包含 刚开始 ...
- hdu1671 字典树记录前缀出现次数
题意: 给你一堆电话号,问你这些电话号后面有没有相互冲突的,冲突的条件是当前这个电话号是另一个电话号的前缀,比如有 123456789 123,那么这两个电话号就冲突了,直接输出NO. 思 ...
- HDU 1251 统计难题(字典树计算前缀数量)
字典树应用,每个节点上对应的cnt是以它为前缀的单词的数量 #include<stdio.h> #include<string.h> struct trie { int cnt ...
- Trie(字典树,前缀树)_模板
Trie Trie,又经常叫前缀树,字典树等等. Trie,又称前缀树或字典树,用于保存关联数组,其中的键通常是字符串.与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定.一个节点的 ...
- HDU 5687 字典树插入查找删除
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...
- HDU 5384 字典树、AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...
- Repository HDU - 2846 字典树
题意:给出很多很多很多很多个 单词 类似搜索引擎一下 输入一个单词 判断有一个字符串包含这个单词 思路:字典树变体,把每个单词的后缀都扔字典树里面,这里要注意dd是一个单词 但是把d 和dd都放字典树 ...
- 字典树(前缀树)-Java实现
字典树 字典树是一种树形结构,优点是利用字符串的公共前缀来节约存储空间.在这提供一个自己写的Java实现,非常简洁. 根节点没有字符路径.除根节点外,每一个节点都被一个字符路径找到. 从根节点到某一节 ...
- HDU 5687 字典树入门
Problem C Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
随机推荐
- some of the properties associated with the solution could not be read解决方法
基于TFS管理的解决方案打开时提示:“some of the properties associated with the solution could not be read”,并不影响项目加载,O ...
- Redux的应该注意的问题
1. Store中的State修改不能直接修改原有的State,若直接修改State,则redux中的所有操作都将指向 内存中的同一个state,将无法获取每一次操作前后的state,就无法追溯sta ...
- X day1
题目pdf 官方题解 T1: 我们可以发现此题若要求$[L,R]$区间的答案,其实就是再求前缀和,我们设$b$为当前出现次数最多的字符,$c$为最小,所以答案为$s[b]_r-s[c]_r-(s[b] ...
- Eclipse NDK 打印LOG信息(都在jni目录下操作)
http://blog.csdn.net/u013045971/article/details/46448975 1 在.c文件中,引用头文件,定义TAG.LOG宏: #include <and ...
- windows下vue项目启动步骤
原创:https://blog.csdn.net/qq_27680317/article/details/71123051?locationNum=10&fps=1 不是ngnix服务器是,忽 ...
- java中静态变量与静态方法的继承(转)
总结: 1.静态变量与静态方法说继承并不确切,静态方法与变量是属于类的方法与变量.而子类也属于超类,比如说Manage extends Employee,则Manage也是一个Employee,所以子 ...
- liunx环境下安装mysql数据库2
mysql的安装和配置[1]解压mysql安装包,进入mysql目录,添加用户,并安装,将权限授权给mysql用户
- JSX 的基本语法规则
JSX 的基本语法规则:遇到 HTML 标签(以 < 开头),就用 HTML 规则解析:遇到代码块(以 { 开头),就用 JavaScript 规则解析
- 动态规划:区间DP与环形DP
区间型动态规划的典型例题是石子归并,同时使用记忆化搜索实现区间动归是一种比较容易实现的方式,避免了循环数组实现的时候一些边界的判断 n堆石子排列成一条线,我们可以将相邻的两堆石子进行合并,合并之后需要 ...
- [洛谷P1707] 刷题比赛
洛谷题目连接:刷题比赛 题目背景 nodgd是一个喜欢写程序的同学,前不久洛谷OJ横空出世,nodgd同学当然第一时间来到洛谷OJ刷题.于是发生了一系列有趣的事情,他就打算用这些事情来出题恶心大家-- ...