Phone List(字典树)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 25709 | Accepted: 7785 |
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:
- 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.
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
题解:只要找到一个word【k】==1,证明这个单词是新的;
就结束,没找到,证明这个单词是其他单词的前缀;
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const int MAXN=;
int ch[MAXN][],word[MAXN];
char dt[MAXN][];
int sz;
void initial(){
sz=;
mem(word,);
mem(ch[],);
}
void join(char *s){
int len=strlen(s),k=,j;
for(int i=;i<len;i++){
j=s[i]-'';
if(!ch[k][j]){
mem(ch[sz],);
ch[k][j]=sz++;
}
k=ch[k][j];
word[k]++;
}
}
bool find(char *s){
int len=strlen(s),k=,j;
for(int i=;i<len;i++){
j=s[i]-'';
k=ch[k][j];
if(word[k]==)return true;
}
return false;
}
void display(int n){
for(int i=;i<n;i++){
if(!find(dt[i])){
// puts(dt[i]);
puts("NO");
return;
}
}
puts("YES");
}
int main(){
int T,N;
scanf("%d",&T);
while(T--){
initial();
scanf("%d",&N);
for(int i=;i<N;i++){
scanf("%s",dt[i]);
join(dt[i]);
}
display(N);
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
const int MAXN=1000010;//要开的足够大
int ch[MAXN][10];
int word[MAXN];
char str[MAXN][15];
int sz;
int N;
void insert(char *s){
int k=0,j;
for(int i=0;s[i];i++){
j=s[i]-'0';
if(!ch[k][j]){
mem(ch[sz],0);
ch[k][j]=sz++;
}
k=ch[k][j];
word[k]++;
}
}
bool find(char *s){
int k=0,j;
for(int i=0;s[i];i++){
j=s[i]-'0';
k=ch[k][j];
if(word[k]==1)return true;
}
return false;
}
bool work(){
for(int i=0;i<N;i++){
// puts("**");
if(!find(str[i]))return false;
}
return true;
}
int main(){
int T;
SI(T);
while(T--){
SI(N);
sz=1;//
mem(ch[0],0);mem(word,0);//
for(int i=0;i<N;i++)scanf("%s",str[i]),insert(str[i]);
if(work())puts("YES");
else puts("NO");
}
return 0;
}
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 ...
- LA 3942 - Remember the Word (字典树 + dp)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
随机推荐
- nginx install lua module
#install luajit #http://luajit.org/download.html .tar.gz cd LuaJIT- make install PREFIX=/home/allen. ...
- C++学习之友元类和友元函数
C++学习之友元类和友元函数 模板类声明也可以有友元,模板的友元可以分为以下几类: 1.非模板友元: 2.约束模板友元,即就是友元的类型取决于类被实例化的时候的 ...
- Android WindowManager 小结
Android---系统服务之 ---WindowManager WindowManager是Android中一个重要的服务(Service ).WindowManager Service 是全局的, ...
- PHP合并数组+与array_merge的区别分析
主要区别是两个或者多个数组中如果出现相同键名,键名分为字符串或者数字,需要注意 1)键名为数字时,array_merge()不会覆盖掉原来的值,但+合并数组则会把最先出现的值作为最终结果返回,而把后面 ...
- python基础学习笔记1
一.字符串: 1.不可变性.分片赋值对于字符串是不合法的. 2.字符串格式化 % eg: print 'The price is: %d' % 30 print 'The price is: %.2f ...
- MFG 常用英文单字
Semiconductor 导体.绝缘体和半导体主要依据导电系数的大小,决定了电子的移动速度. 导体:金.银.铜.铁.人.水……导电系数大,传导容易 绝缘体:塑料.木头.皮革.纸……导电系数小.传导不 ...
- 实现将VirtualBox 虚拟机转换为KVM虚拟机的步骤
原来在桌面上一直使用virtualbox虚拟机管理程序(VMM)构建虚拟机安装不同的操作系统,现在 研究linux下的KVM,能否将已经建立的virtualBox虚拟客户机(guest)转换为KVM虚 ...
- PE头的应用---插入代码到EXE或DLL文件中
三.代码实现(DELPHI版本),采用第三种方式实现代码插入. 1. 定义两个类,一个用来实现在内存中建立输入表:一个用来实现对PE头的代码插入. DelphiCode: const MAX_SECT ...
- css table 布局
使用CSS表格 CSS表格能够解决所有那些我们在使用绝对定位和浮动定位进行多列布局时所遇到的问题.例如,“display:table;”的CSS声明能够让一个HTML元素和它的子节点像table元素一 ...
- 如何从 0 开始学 ruby on rails (漫步版)
如何从 0 开始学 ruby on rails (漫步版) ruby 是一门编程语言,ruby on rails 是 ruby 的一个 web 框架,简称 rails. 有很多人对 rails 感兴 ...