Phone List POJ 3630 Trie Tree 字典树
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 29416 | Accepted: 8774 |
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<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 10003
#define LLL 1000000000
#define INF 1000000009
#define eps 0.00000001
/*
给定一些字符串,求这些字符串中是否存在一个字符串是另一个字符串的前缀
用trie tree解决
在插入时,分两种情况:
1.当前字符串的长度小于Tree中字符串长度 ,s[i] == '\0'的情况
2.和当前字符串长度大于或等于Tree中字符串长度,在Tree中每个字符串结束的地方标记一下!
*/
bool flag,first;//这个标志是不是已经产生 上述情况
char s[MAXN];
typedef struct TreeNode
{
bool Endflag;
struct TreeNode* Next[];
}*Tree;
TreeNode Memory[];
int alloc = ;
Tree Newnode()
{
Tree T = &Memory[alloc++];
for (int i = ; i < ; i++)
{
//T->Next[i] = (Tree)malloc(sizeof(TreeNode));
T->Next[i] = NULL;
T->Endflag = false;
}
return T;
}
Tree Insert(char s[], Tree t)
{
int p = ;
Tree T = t;
while (s[p])
{
int k = s[p] - '';
if (!T->Next[k])
T->Next[k] = Newnode();
else
{
if (T->Next[k]->Endflag||(!T->Next[k]->Endflag&&s[p+]=='\0'))
{
flag = true;
return t;
}
}
T = T->Next[k];
p++;
}
T->Endflag = true;
return t;
}
int main()
{
int cas;
scanf("%d", &cas);
while (cas--)
{
Tree T = Newnode();
flag = false; first = true;
int n;
scanf("%d", &n);
while (n--)
{
scanf("%s", s);
if (flag) continue;
Insert(s, T);
}
if (!flag)
printf("YES\n");
else
printf("NO\n");
}
return ;
}
Phone List POJ 3630 Trie Tree 字典树的更多相关文章
- poj 3630 Phone List(字典树)
题目链接: http://poj.org/problem?id=3630 思路分析: 求在字符串中是否存在某个字符串为另一字符串的前缀: 即对于某个字符串而言,其是否为某个字符串的前缀,或存在某个其先 ...
- Trie(字典树)
没时间整理了,老吕又讲课了@ @ 概念 Trie即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种,典型应用是统计和排序大量的字符串(不限于字符串) Trie字典树主要用于存储字符串, ...
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- nyoj 163 Phone List(动态字典树<trie>) poj Phone List (静态字典树<trie>)
Phone List 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Given a list of phone numbers, determine if it i ...
- 【leetcode】208. Implement Trie (Prefix Tree 字典树)
A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...
- 208 Implement Trie (Prefix Tree) 字典树(前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个方法.注意:你可以假设所有的输入都是小写字母 a-z.详见:https://leetcode.co ...
- [LintCode] Implement Trie 实现字典树
Implement a trie with insert, search, and startsWith methods. Have you met this question in a real i ...
- Trie - leetcode [字典树/前缀树]
208. Implement Trie (Prefix Tree) 字母的字典树每个节点要定义一个大小为26的子节点指针数组,然后用一个标志符用来记录到当前位置为止是否为一个词,初始化的时候讲26个子 ...
- poj 1056 IMMEDIATE DECODABILITY 字典树
题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...
随机推荐
- bzoj5130: [Lydsy1712月赛]字符串的周期
这道题很有意思啊. 字符串循环节用KMP(手推一下) 假如是26^12肯定很不滋磁 但是可以发现ABA和BCB和BAB这些都是等价的 那就把最小的拿出来搞再乘个排列数就好了 #include<c ...
- java-com-util-common-service:BaseService.java
ylbtech-java-com-util-common-service:BaseService.java 1.返回顶部 1. package com.shineyoo.manager.util.co ...
- Java-JDK:JDK百科
ylbtech-Java-JDK:JDK百科 JDK是 Java 语言的软件开发工具包,主要用于移动设备.嵌入式设备上的java应用程序.JDK是整个java开发的核心,它包含了JAVA的运行环境(J ...
- LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)
这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...
- APP加固反调试(Anti-debugging)技术点汇总
0x00 时间相关反调试 通过计算某部分代码的执行时间差来判断是否被调试,在Linux内核下可以通过time.gettimeofday,或者直接通过sys call来获取当前时间.另外,还可以通过自定 ...
- xcode制作越狱后ipa安装文件
正常情况下发布测试版给用户需要问到对方设备ID并添加到开发者证书里去感觉有点麻烦,如果是已越狱过的机器可以使用xcode制作ipa文件,并直接用itunes同步进去,这样方便多了. 将运行目标选为iO ...
- css的外边距合并或者外边距塌陷问题
第一种情况: 已知两个宽和高均为100px,margin均为20px的div垂直排列,现象如下图所示: 当设置css1的margin-bottom:40px:或者css2的margin-top:40p ...
- Mysql Event 自动分表
create table TempComments Like dycomments; 上述 SQL语句创建的新表带有原表的所有属性,主键,索引等. 自动分表怎么做呢? 使用上述语句自动创建分表. 那么 ...
- 后端springmvc,前端html5的FormData实现文件断点上传
前言 最近项目中有使用到文件断点上传,得空便总结总结,顺便记录一下,毕竟“好记性不如烂笔头”. 后端代码: package com.test.controller; import java.io.Bu ...
- Embedded之Stack之一
1 Intro When a program starts executing, a certain contiguous section of memory is set aside for the ...