Phone List
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 字典树的更多相关文章

  1. poj 3630 Phone List(字典树)

    题目链接: http://poj.org/problem?id=3630 思路分析: 求在字符串中是否存在某个字符串为另一字符串的前缀: 即对于某个字符串而言,其是否为某个字符串的前缀,或存在某个其先 ...

  2. Trie(字典树)

    没时间整理了,老吕又讲课了@ @ 概念 Trie即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种,典型应用是统计和排序大量的字符串(不限于字符串) Trie字典树主要用于存储字符串, ...

  3. POJ 2001 Shortest Prefixes(字典树)

    题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...

  4. 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 ...

  5. 【leetcode】208. Implement Trie (Prefix Tree 字典树)

    A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...

  6. 208 Implement Trie (Prefix Tree) 字典树(前缀树)

    实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个方法.注意:你可以假设所有的输入都是小写字母 a-z.详见:https://leetcode.co ...

  7. [LintCode] Implement Trie 实现字典树

    Implement a trie with insert, search, and startsWith methods. Have you met this question in a real i ...

  8. Trie - leetcode [字典树/前缀树]

    208. Implement Trie (Prefix Tree) 字母的字典树每个节点要定义一个大小为26的子节点指针数组,然后用一个标志符用来记录到当前位置为止是否为一个词,初始化的时候讲26个子 ...

  9. poj 1056 IMMEDIATE DECODABILITY 字典树

    题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...

随机推荐

  1. 海思3518e mpp2/sample/venc makefile简析

    http://blog.csdn.net/u011003120/article/details/51324567

  2. write data to xml

    public class Student { public int Id { get; set; } public string FirstName { get; set; } public stri ...

  3. Base Class Doesn't Contain Parameterless Constructor?

    http://stackoverflow.com/questions/7689742/base-class-doesnt-contain-parameterless-constructor #regi ...

  4. poi读取word2003(.doc文档)中的表格

    poi读取word2003(.doc文档)中的表格 Jakarta POI 是apache的子项目,目标是处理ole2对象.它提供了一组操纵Windows文档的Java API.在网上见到好多通过po ...

  5. Head First 设计模式 —— 策略设计模式

    创建一个能够根据所传递的参数对象的不同而具有不同行为(动态绑定的多态机制)的方法,被称为策略设计模式.

  6. Coursera Algorithms week3 快速排序 练习测验: Nuts and bolts

    题目原文: Nuts and bolts. A disorganized carpenter has a mixed pile of n nuts and n bolts. The goal is t ...

  7. Appium + python - online-install-apk

    import osfrom appium import webdriver# 安装app,为了方便,把app放到当前脚本同一目录os.system("adb install sina.apk ...

  8. AOP实现参数的判空问题

    不想每次都去判断必传的参数是否为空,写代码太繁琐了,正好最近用了AOP实现权限控制,依葫芦画瓢,现在用它实现参数的判空,至于AOP的原理之类,自己百度了解一下吧 1. NullDisable注解 @D ...

  9. A - HQ9+

    Problem description HQ9+ is a joke programming language which has only four one-character instructio ...

  10. Redis hash结构 和常用命令

    Redis 数据结构 -- 哈希 hash 是 一个 String 类型的field 和 value 的映射表 hash 的键值 对在内存中的一种无序的状态 命令 说明 备注 hdel key fie ...