Phone List
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16341   Accepted: 5228

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

Source

【分析】字典树

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)+1)
using namespace std;
typedef long long ll;
const int N=;
const int M=1e6+;
typedef struct TrieNode {
int endflag;
struct TrieNode *next[N];
} TrieNode; TrieNode Memory[M];
int allocp=;
bool flag; void InitTrieRoot(TrieNode **pRoot) {
*pRoot=NULL;
} TrieNode *CreateTrieNode() {
int i;
TrieNode *p; p=&Memory[allocp++];
p->endflag=;
for(i=; i<N; i++) {
p->next[i]=NULL;
}
return p;
} void InsertTrie(TrieNode **pRoot,char *s) {
int i,k;
TrieNode *p; if(!(p=*pRoot))
p=*pRoot=CreateTrieNode();
i=;
while(s[i]) {
k=s[i++]-'';
if(p->next[k]) {
if(p->next[k]->endflag==||s[i]=='\0') {
flag=false;
return;
}
} else p->next[k]=CreateTrieNode();
p=p->next[k];
}
p->endflag=;
} int main() {
char str[];
TrieNode *Root=NULL;
int T;
scanf("%d",&T);
int n;
while(T--) {
flag=true;
allocp=;
scanf("%d",&n);
InitTrieRoot(&Root);
for(int i=; i<n; i++) {
//gets(str);//用这个会超时
scanf("%s",&str);
if(flag) InsertTrie(&Root,str);
}
if(flag) printf("YES\n");
else printf("NO\n");
} return ;
}

UVA Phone List (字典树)(查询是否有前缀或自身是其他的前缀)的更多相关文章

  1. CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)

    题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种: + x: 表示向集合中添加一个元素x - x:表示删除集合中值为x的一个元素 ? x:表示查询集合中与x异或的最大 ...

  2. UVALive 7712 Confusing Manuscript 字典树 查询与s的编辑距离为1的字符串数量

    /** 题目:UVALive 7712 Confusing Manuscript 链接:https://vjudge.net/problem/UVALive-7712 题意:给定n个不同的字符串,f( ...

  3. UVA 11732 链表+字典树

    因为字符集比较大,所以就不能用简单字典树,在字典树里面,用链表进行存储.这个倒是不难,练了下手 统计的时候还是有点难搞,因为要算所有的两两比较的次数之和,对分叉处进行计算,注意细节 #include ...

  4. [数据结构]字典树(Tire树)

    概述: Trie是个简单但实用的数据结构,是一种树形结构,是一种哈希树的变种,相邻节点间的边代表一个字符,这样树的每条分支代表一则子串,而树的叶节点则代表完整的字符串.和普通树不同的地方是,相同的字符 ...

  5. poj 3764 The xor-longest Path(字典树)

    题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根 ...

  6. 数据结构~trie树(字典树)

    1.概述 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. 我理解字典树是看了这位大佬博客.还不了解字典树的 ...

  7. 字典树(前缀树)-Java实现

    字典树 字典树是一种树形结构,优点是利用字符串的公共前缀来节约存储空间.在这提供一个自己写的Java实现,非常简洁. 根节点没有字符路径.除根节点外,每一个节点都被一个字符路径找到. 从根节点到某一节 ...

  8. 数据结构&字符串:01字典树

    利用01字典树查询最大异或值 01字典树的是只含有0和1两种字符的字典树,在使用它的时候,把若干数字转成二进制后插入其中 在查询树中的哪个数字和给定数字有最大异或值的时候,从根开始贪心查询就ok了 H ...

  9. hust 1605 - Gene recombination(bfs+字典树)

    1605 - Gene recombination Time Limit: 2s Memory Limit: 64MB Submissions: 264 Solved: 46 DESCRIPTION ...

  10. 字典树Trie Tree

    又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:利用字符串的公共前缀 ...

随机推荐

  1. ACM International Collegiate Programming Contest, Egyptian Collegiate Programming Contest (ECPC 2015)

    A.Arcade Game(康拓展开) 题意: 给出一个每个数位都不同的数n,进行一场游戏.每次游戏将n个数的每个数位重组.如果重组后的数比原来的数大则继续游戏,否则算输.如果重组后的数是最大的数则算 ...

  2. Ubuntu下安装LNMP之nginx的卸载

    我在安装Nginx时,是采用自己从网上down下自己需要的nginx版本进行编译安装的,如果使用过apt库来进行安装的话可以参考这篇文章:ubuntu中彻底删除nginx 假如是编译安装的童鞋,可以按 ...

  3. HNOI2002 彩票 [搜索]

    题目描述 某地发行一套彩票.彩票上写有1到M这M个自然数.彩民可以在这M个数中任意选取N个不同的数打圈.每个彩民只能买一张彩票,不同的彩民的彩票上的选择不同. 每次抽奖将抽出两个自然数X和Y.如果某人 ...

  4. 用JSR的@Inject代替@Autowired完成自动装配

    从spring3.0开始spring支持JSR-330 的标准注解.主要是javax.inject这个包下的: 下面的例子用@Inject代替@Autowired.完成自动装配: MovieFinde ...

  5. 使用T4模板生成MySql数据库实体类

    注:本文系作者原创,但可随意转载. 现在呆的公司使用的数据库几乎都是MySQL.编程方式DatabaseFirst.即先写数据库设计,表设计按照规范好的文档写进EXCEL里,然后用公司的宏,生成建表脚 ...

  6. javascript中Date使用总结(转)

    //全局函数 Date //Date 类的静态方法 Date.parse Date.UTC //Date 对象的建立方法 new Date() new Date(毫秒数) new Date(标准时间格 ...

  7. HDU 2554 N对数的排列问题 ( 数学 )

    题目链接 Problem Description 有N对双胞胎,他们的年龄分别是1,2,3,--,N岁,他们手拉手排成一队到野外去玩,要经过一根独木桥,为了安全起见,要求年龄大的和年龄小的排在一起,好 ...

  8. Educational Codeforces Round 40 A B C D E G

    A. Diagonal Walking 题意 将一个序列中所有的\('RU'\)或者\('UR'\)替换成\('D'\),问最终得到的序列最短长度为多少. 思路 贪心 Code #include &l ...

  9. kuangbin带你飞 最短路 题解

    求一个图最短路边的办法.好像下面的那个有问题.单向边和双向边一定是有区别的.这个比较容易.参照该文的最短路网络流题目和连通图题目一题求最短路关节边 另外上述2个题目的代码好像有问题. 在UVALIVE ...

  10. (二十八)fopen与读写的标识r,r+,rb+,rt+,w+.....

    fopen与读写的标识r,r+,rb+,rt+,w+..... 函数简介 函数功能: 打开一个文件 函数原型:FILE * fopen(const char * path,const char * m ...