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. mssql字符串分割后的值,把表中不存在的插入表中

    字符串分割后的值,把表中不存在的插入表中 --供大家参考 使用场景,自行思考…… --创建表tb1 Create table tb1 ( cola int, colb ) ) --插入数据 inser ...

  2. easyui 验证控件 tooltip message显示位置

    找了半天才发现是这个属性在控制,tipPosition:'left',官网那个demo,误人子弟.

  3. mysql 简单游标

    <=====================MYSQL 游标示例=====================> CREATE PROCEDURE `test`.`new_procedure` ...

  4. .net mvc笔记4_依赖注入

    一.Building Loosely Coupled Components MVC模式最重要的特点就是关注点分离.我们希望应用中的组件能尽可能的独立,相互之间即使有依赖也要在我们的控制之下. 在理想情 ...

  5. python进阶6 HTTP协议客户端实现

    httplib 1.httplib 是 python中http 协议的客户端实现,可以使用该模块来与 HTTP 服务器进行交互. httplib的内容不是很多,也比较简单.以下是一个非常简单的例子,使 ...

  6. Oracle字符集转换

            这几天在工作中碰到一个字符乱码的问题,发现在cmd窗口的sqlplus中直接update一个中文和使用@调用一个文件作同样更新的时候,存储的结果 竟不一样.一时比较迷惑,对Oracle ...

  7. m文件转换为C/C++文件的编译、绘图、参数、打包问题总结

    在工程计算相关项目中,常常利用Matlab来完成计算.算法.绘图等功能.使用Matlab来完成这些功能非常简单,Matlab提供的m编程语言功能强大,代码量少.为了在自己的C/C++项目中加入这些功能 ...

  8. Codeforces 427D Match &amp; Catch(后缀自动机)

    [题目链接] http://codeforces.com/problemset/problem/427/D [题目大意] 给出一个两个字符串,求出最短且在两个字符串中唯一的公共子串. [题解] 以原字 ...

  9. iPhone 6 为何坚持1GB内存?

    原文地址:http://digi.ifeng.com/expert/special/96/#6467378-qzone-1-9015-46cf52f061fd6e814686a918cedcb024 ...

  10. CentOS 6.4 U盘启动盘制作、安装及遇到的问题解决

    用UltraISO Premium Edition  9.3 制作的CentOS 6.4 U盘安装盘, 制作过程參考我写的百度经验:UltraISO制作U盘系统盘安装CentOS经验分享 安装时提示P ...