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. DirectFB简介以及移植[一]

    本文转载自‘:http://blog.csdn.net/wavemcu/article/details/39251805 版权声明:本文为博主原创文章,未经博主允许不得转载. ************ ...

  2. Socket之shutdown()用法

    通常来说,socket是双向的,即数据是双向通信的.但有些时候,你会想在socket上实现单向的socket,即数据往一个方向传输. 单向的socket便称为半开放Socket.要实现半开放式,需要用 ...

  3. B1051 受欢迎的牛 tarjan缩点

    就是一道tarjan缩点的板子,之前在洛谷做过.但是我发现一个事,就是函数里面有一句话: void tarjan(int x) { dfn[x] = low[x] = ++tot; str[++top ...

  4. succ

  5. js中 if不判断解决方式

    $(function() { $("#number").blur(function() { var number = $('#number').val(); var num = $ ...

  6. 快速搭建ELK集中化日志管理平台

    由于我们的项目是分布式,服务分布于多个服务器上,每次查看日志都要登录不同服务器查看,而且查看起来还比较麻烦,老大让搭一个集中化日志管理的东西,然后就在网上找到了这个东西ELK ELK就是elastic ...

  7. Winform 异步调用2 时间

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. 用xftp从win7系统传输一些必要的文件到Linux

    新建会话,主机名为Linux系统的ip地址,选用SFTP协议,选用UTF-8编码格式 1.安装JDK 切换到java路径下 卸载openJDK: 用rpm -qa |grep java指令查看 用rp ...

  9. Oracle备份Scott

    @echo off echo ================================================ echo Windows环境下Oracle数据库的自动备份脚本 echo ...

  10. django的admin后台管理如何更改为中文

    新建Django的admin后端控制为英文显示,为了可以使其显示中文,可以将 setting.py配置文件修改 # LANGUAGE_CODE = 'en-us' # # # # # TIME_ZON ...