nyoj163_Phone List_字典树
Phone List
- 描述
-
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:
- Emergency 911
- Alice 97 625 999
- 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.
- 输入
- The first line of input gives a single integer, 1 ≤ t ≤ 10, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 ≤ n ≤ 100000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.
- 输出
- For each test case, output "YES" if the list is consistent, or "NO" otherwise.
- 样例输入
-
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346 - 样例输出
-
NO
YES
解题思路:刚开始直接看测试数据差点误解,以为是按顺序存储的就输出yes呢。。。。仔细翻译才明白。
字典树,就是按照一定的顺序来构造一棵树,对于很多字符串来说,有很多重复的前缀,那么相同的前缀就不用都存储一遍了,用一个相同前缀来存储就可以。
此题判断是否有前缀有两种情况(每次都是按位存),① 如果挨着往里存储,当此时的字符串a都存储完的时候还没有建立新的节点,说明在此之前已经有一个字符串b了,所以a是b的前缀。
② 当这个a字符串还没有存储完成就碰到endd=1,说明之前有一个字符串b已经结束了,b是这个a的前缀。
代码:
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; typedef struct tree{
int endd;
tree *next[];
tree(){
endd=;
memset(next,NULL,sizeof(next));
}
};
tree *root; int inser(char a[]){
int flag=;
tree *p=root;
int k;
int len=strlen(a);
for(int i=;i<len;i++){
k=a[i]-'';
if(p->next[k]==NULL){
p->next[k]=new tree;
flag=;
}
p=p->next[k];
if(p->endd){
return ;
}
}
p->endd=;
if(!flag){
return ;
}
return ;
} int main()
{
int t;
char a[];
scanf("%d",&t);
while(t--){
int n;
int flag=;
root=new tree();
scanf("%d",&n);
while(n--){
scanf("%s",a);
if(flag){
flag=inser(a);
}
}
if(!flag){
printf("NO\n");
}else{
printf("YES\n");
}
}
return ;
}
nyoj163_Phone List_字典树的更多相关文章
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树+博弈 CF 455B A Lot of Games(接龙游戏)
题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
- 山东第一届省赛1001 Phone Number(字典树)
Phone Number Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 We know that if a phone numb ...
- 字典树 - A Poet Computer
The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...
- trie字典树详解及应用
原文链接 http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用 一.知识简介 ...
- HDU1671 字典树
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- *HDU1251 字典树
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
随机推荐
- PHP中spl_autoload_register()函数
spl_autoload_register — 注册给定的函数作为 __autoload 的实现 官方地址:http://php.net/manual/zh/function.spl-autoload ...
- POJ 2948 Martian Mining
Martian Mining Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2251 Accepted: 1367 Descri ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- Hibernate面试题
一.谈一谈Hibernate的缓存机制 1.一级缓存: 内部缓存存在于HIbernate中又叫一级缓存,他属于应用事务级缓存. 2.二级缓存: 01.应用级缓存. 02.分布式缓存. 条件:数据不会被 ...
- 线程7-ThreadLocal
有时间再整理 ThreadLocal不是用来解决对象共享访问问题的,而主要是提供了保持对象的方法和避免参数传递的方便的对象访问方式.归纳了两点: 1.每个线程中都有一个自己的ThreadLocalMa ...
- java中重载与重写的区别
(1) 方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型. 重载Overloading是一个类中多态性的一种表现. 然后我们再来谈谈 重写(Over ...
- 淘宝(阿里百川)手机客户端开发日记第十一篇 JSP+Servlet
由于本人从事.net开发已有多年经验,今天由于工作需要,我只能学习下JSP+Servlet,至于java web提供了更好的开发框架MVC,现在由于时间关系,我只好用JSP+Servlet来搭建服务器 ...
- windows程序是如何开始执行的??
windows的资源管理器侦测到使用者执行了一个程序————>windows调用加载器加载该程序————>调用C start code——>C start code 调用WinMai ...
- python 函数式编程工具
有三个内置函数与列表一起使用时非常有用:filter().map()和reduce(). 1. filter(function, sequence)返回的序列由function(item)结果为真的元 ...
- C++虚函数、虚继承、对象内存模型(转)
参考:http://blog.csdn.net/hxz_qlh/article/details/14633361 需要注意的是虚继承.多重继承时类的大小.