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树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:利用字符串的公共前缀 ...
随机推荐
- [poj] 2396 [zoj] 1994 budget || 有源汇的上下界可行流
poj原题 zoj原题 //注意zoj最后一行不要多输出空行 现在要针对多赛区竞赛制定一个预算,该预算是一个行代表不同种类支出.列代表不同赛区支出的矩阵.组委会曾经开会讨论过各类支出的总和,以及各赛区 ...
- Notice : brew install php70
To enable PHP in Apache add the following to httpd.conf and restart Apache: LoadModule php7_module ...
- [Leetcode] The minimum depth of binary tree二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [Leetcode] unique paths 独特路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [学习笔记]最小割之最小点权覆盖&&最大点权独立集
最小点权覆盖 给出一个二分图,每个点有一个非负点权 要求选出一些点构成一个覆盖,问点权最小是多少 建模: S到左部点,容量为点权 右部点到T,容量为点权 左部点到右部点的边,容量inf 求最小割即可. ...
- word公式编辑中的转义字符
Some of the commonly used symbols: \infty - Infinity \leq - Less then or equal \geq - ...
- Codeforces Round #525 (Div. 2)D. Ehab and another another xor problem
D. Ehab and another another xor problem 题目链接:https://codeforces.com/contest/1088/problem/D Descripti ...
- Saruman’s Level Up~(多校赛算组合数)
Description Saruman’s army of orcs and other dark minions continuously mine and harvest lumber out o ...
- 在WPF中应用弱事件模式
http://www.cnblogs.com/rickiedu/archive/2007/03/15/676021.html 在wpf中应用弱事件模式 感谢VS 的Intellisens ...
- 查看jar包源码
1.Maven项目 如下图设置后,它会自动下载源文件,这样就能看到了 2.传统Java项目 2.1.安装 Jad 下载 Jad :http://varaneckas.com/jad/ 解压到任意目录即 ...