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类型的变 ...
随机推荐
- 使用printf函数实现串口信息打印——设置IAR和Keil的Options
在Keil和IAR中都可以使用printf函数,但两者设置的方法不一样.以下分别是IAR和Keil的设置. 下面是Keil的设置. 选中Options--->Target--->Code ...
- Android4.4 wpa_supplicant深入分析之wpa_supplicant初始化流程续
下面我们将接上一篇文章继续分析main中第二个关键函数wpa_supplicant_add_iface. wpa_supplicant_add_iface用于向wpa_supplicant添加接口设备 ...
- UESTC--1253--阿里巴巴和n个大盗 (博弈)
阿里巴巴和n个大盗 Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & %llu Submit St ...
- git 设定全局ignore
创建: 2017/08/08 位置: $HOME/.config/git/ignore git/ignore 要自建 内容 https://github.com/github/gitignore ...
- hdu3652B-number(数位dp)
B-number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- MySQL实现递归查询
DROP FUNCTION IF EXISTS queryChildrenCaseInfo;CREATE FUNCTION queryChildrenCaseInfo(cId INT)RETURNS ...
- 我和CSDN的那些事
作者:朱金灿 来源:http://blog.csdn.net/clever101 前些日子收到这样一个邀请: CSDN的工作人员还来电给我确认是否能参加.开始我有点犹豫,毕竟是在工作日的晚上,毕竟离我 ...
- OpenCL C
OpenCL C OpenCL 简介 opencl C是ISO C99的一个扩展,主要区别如下: 去除了C99的一些特性,如:标准C99头文件,函数指针,递归,变长数组,和位域 增加了一些特性用于并 ...
- 控制台输入年龄,根据年龄输出不同的提示 ------if……else if ……else 语句
package com.zuoye.test; import java.util.Scanner; public class Nianling { public static void main(St ...
- 【Linux】创建逻辑卷管理(LVM)
LVM是对磁盘进行分区管理的机制.LVM有很多优点:在线扩容,跨磁盘分区......,缺点:管理相对麻烦.创建LVM的过程如下: LVM是基于普通分区或者整块硬盘来进行的.我们首先把这些存储转换为PV ...