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(字典树)的更多相关文章

  1. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  2. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  3. 字典树+博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

  4. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  5. 山东第一届省赛1001 Phone Number(字典树)

    Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We know that if a phone numb ...

  6. 字典树 - A Poet Computer

    The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...

  7. trie字典树详解及应用

    原文链接    http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用   一.知识简介        ...

  8. HDU1671 字典树

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. *HDU1251 字典树

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  10. LA 3942 - Remember the Word (字典树 + dp)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

随机推荐

  1. 选项卡 js操作

    html代码展示(这里展示的是关于日程的标签页)css样式这里省略了>>>>自己写的可能更好看 <div class="row"> <ul ...

  2. 搭建Hadoop集群 (二)

    前面的步骤请看  搭建Hadoop集群 (一) 安装Hadoop 解压安装 登录master, 下载解压hadoop 2.6.2压缩包到/home/hm/文件夹. (也可以从主机拖拽或者psftp压缩 ...

  3. Mysql 死锁问题

    Innodb锁系统(4) Insert/Delete 锁处理及死锁示例分析 http://mysqllover.com/?p=431 关于innodb死锁 http://afei2.sinaapp.c ...

  4. (转)深入浅出Java三大框架SSH与MVC的设计模式

    原址:http://www.educity.cn/java/1382738.html 现在许许多多的初学者和程序员,都在趋之若鹜地学习Web开发的宝典级框架:Struts2, Spring,Hiber ...

  5. glib源码安装使用方法

    glib库是GTK+和GNOME工程的基础底层核心程序库,是一个综合用途的实用的轻量级的C程序库,它提供C语言的常用的数据结构的定义.相关的处理函数,有趣而实用的宏,可移植的封装和一些运行时机能,如事 ...

  6. 7_Table Views

    7 // // ViewController.swift // Table Views // // Created by ZC on 16/1/9. // Copyright © 2016年 ZC. ...

  7. mysql null值问题

    mysql> create table test( sn int, -> `createdTime` datetime NOT NULL COMMENT '创建时间', -> `up ...

  8. C++读写文件流的相关介绍

    掌握文本文件读写的方法了解二进制文件的读写方法 C++文件流:fstream // 文件流ifstream  // 输入文件流ofstream  // 输出文件流 //创建一个文本文件并写入信息//同 ...

  9. IE 中开发,兼容与性能测试工具汇总

    前言 对于开发者来说, IE的兼容性是最让人头疼的. 因为是微软的产品, 且绑定在操作系统上, 所以IE的占用率还是相当大, 对于开发者来说, 这部分的兼容的考虑就不可避免了. 对于IE 的各版本来说 ...

  10. Linux 电子书共享下载--大家一起学习

    文件名 大小 时间 到期时间 操作    鸟哥私房菜(全集).pdf 36.57 MB 2 小时前 免费永久       练成Linux高手.chm 3.76 MB 2 小时前 免费永久        ...