UVA 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 (字典树)(查询是否有前缀或自身是其他的前缀)的更多相关文章
- CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)
题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种: + x: 表示向集合中添加一个元素x - x:表示删除集合中值为x的一个元素 ? x:表示查询集合中与x异或的最大 ...
- UVALive 7712 Confusing Manuscript 字典树 查询与s的编辑距离为1的字符串数量
/** 题目:UVALive 7712 Confusing Manuscript 链接:https://vjudge.net/problem/UVALive-7712 题意:给定n个不同的字符串,f( ...
- UVA 11732 链表+字典树
因为字符集比较大,所以就不能用简单字典树,在字典树里面,用链表进行存储.这个倒是不难,练了下手 统计的时候还是有点难搞,因为要算所有的两两比较的次数之和,对分叉处进行计算,注意细节 #include ...
- [数据结构]字典树(Tire树)
概述: Trie是个简单但实用的数据结构,是一种树形结构,是一种哈希树的变种,相邻节点间的边代表一个字符,这样树的每条分支代表一则子串,而树的叶节点则代表完整的字符串.和普通树不同的地方是,相同的字符 ...
- poj 3764 The xor-longest Path(字典树)
题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根 ...
- 数据结构~trie树(字典树)
1.概述 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. 我理解字典树是看了这位大佬博客.还不了解字典树的 ...
- 字典树(前缀树)-Java实现
字典树 字典树是一种树形结构,优点是利用字符串的公共前缀来节约存储空间.在这提供一个自己写的Java实现,非常简洁. 根节点没有字符路径.除根节点外,每一个节点都被一个字符路径找到. 从根节点到某一节 ...
- 数据结构&字符串:01字典树
利用01字典树查询最大异或值 01字典树的是只含有0和1两种字符的字典树,在使用它的时候,把若干数字转成二进制后插入其中 在查询树中的哪个数字和给定数字有最大异或值的时候,从根开始贪心查询就ok了 H ...
- hust 1605 - Gene recombination(bfs+字典树)
1605 - Gene recombination Time Limit: 2s Memory Limit: 64MB Submissions: 264 Solved: 46 DESCRIPTION ...
- 字典树Trie Tree
又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:利用字符串的公共前缀 ...
随机推荐
- MySQL之SELECT 语句详解
本文参考实验楼的SELECT 语句详解结合自己操作部分而写成. 注意:大多数系统中,SQL语句都是不区分大小写的,但是出于严谨和便于区分保留字和变量名,在书写的时,保留字应大写,而变量名应小写.所谓的 ...
- bzoj 1977 洛谷P4180 严格次小生成树
Description: 给定一张N个节点M条边的无向图,求该图的严格次小生成树.设最小生成树边权之和为sum,那么严格次小生成树就是边权之和大于sum的最小的一个 Input: 第一行包含两个整数N ...
- recycleview的基础Adapter
.封装了一个基础的adapter.,用于recycleview的快捷使用有BaseAdapter,BaseViewHolder,PAdapter,MainActivity public abstrac ...
- 关于javascript中的this 一段小实例深有体会啊
先声明鄙人正在努力的把脚抬进门来,说的都是比较粗浅的知识,但都是我实践中得出的体会,很深刻. 正在自学中挣扎的DOG. 先看段代码: function highlightRows() { if(!d ...
- 2、Distributed Optimization
一.目录: Distributed dynamic programming (as applied to path-planning problems). Distributed solutions ...
- 转:mybatis 高级结果映射(http://blog.csdn.net/ilovejava_2010/article/details/8180521)
高级结果映射 MyBatis的创建基于这样一个思想:数据库并不是您想怎样就怎样的.虽然我们希望所有的数据库遵守第三范式或BCNF(修正的第三范式),但它们不是.如果有一个数据库能够完美映射到所有应用程 ...
- MongoDB简介以及下载安装
什么是MongoDB ? MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.运行稳定,性能高 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在 ...
- 使用QML创建第一个界面(转)
原文转自 https://blog.csdn.net/rl529014/article/details/51378307 在Qt编程中,我们可以使用纯C++代码,或C++和XML结合的方式来创建GUI ...
- USB接口无法识别设备
http://windows.microsoft.com/zh-cn/windows/answers?tId=14fa1e44-0a19-48ef-9ba7-b7e512a837a4 小琼子 提问 2 ...
- Oracle基础 08 用户角色 user/role
--查询帐户的状态select username,account_status from dba_users where username='SCOTT'; --创建用户create user jo ...