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添加删除元素

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  2. php ZIP压缩类实例分享

    php ZIP压缩类实例分享 <?php $zipfiles =array("/root/pooy/test1.txt","/root/pooy/test2.txt ...

  3. codevs 1256 打鼹鼠 LIS

    题目链接 题目描述 Description 鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的. 根据这个特点阿Q编写了一个打鼹鼠的游戏:在一个n*n的网格中,在某些时 ...

  4. 《Pointers On C》读书笔记(第五章 操作符和表达式)

    1.C语言操作符优先级表 2.算术操作符中%(取模操作符)只适用于整型类型,其余几个操作符(+.-.*./)既适用于整型类型也适用于浮点类型.当/操作符的两个操作数都是整型时,它执行整除运算,其它情况 ...

  5. 5_Navigation Bar

    5 // // ViewController.swift // Navigation Bar // // Created by ZC on 16/1/9. // Copyright © 2016年 Z ...

  6. Java常用类库--观察者设计模式( Observable类Observer接口)

    如果要想实现观察者模式,则必须依靠java.util包中提供的Observable类和Observer接口. import java.util.* ; class House extends Obse ...

  7. HDU 5730 Shell Necklace(CDQ分治+FFT)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5730 [题目大意] 给出一个数组w,表示不同长度的字段的权值,比如w[3]=5表示如果字段长度为3 ...

  8. HDU 5782 Cycle(KMP+Hash)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5782 [题目大意] 给出两个字符串,判断他们每一个前缀是否循环同构,循环同构的意思就是,字符串首位 ...

  9. Kate Spade_百度百科

    Kate Spade_百度百科 Kate Spade

  10. VIPS: a VIsion based Page Segmentation Algorithm

    VIPS: a VIsion based Page Segmentation Algorithm VIPS: a VIsion based Page Segmentation Algorithm In ...