Phone List
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 25280   Accepted: 7678

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

为何感觉一用指针就各种力不从心?

题意:t组实例,每组n个号码,判断里面号码是否有谁是谁的前缀,如果有则输出”NO“,否则“YES”;

亮点:插入新单词时给每个分支的结束处加一个结束标志,那么便出现了两种情况1.之前单词顺着这个分支走还没结束,而新插入的单词到这里结束了,说明新单词与旧单词重叠,则说明冲突,2.新插入单词未结束,而旧单词在这个节点结束了,说明旧单词是新单词的前缀,判断直接结束,冲突了~

 #include<iostream>
#include<vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <math.h>
#include<algorithm>
#define ll long long
#define eps 1e-8
using namespace std;
int p;
struct nodes
{
int cnt;
struct nodes *next[];
}* root,*temp,treenodes[];//静态申请节点比较省时间,也可以不用释放内存 int inserts(char word[])
{
nodes *cur = root;
int i = ;
while(word[i] )
{
int t = word[i] - '';
if(cur->next[t] )
{
if(cur->next[t]->cnt == || word[i + ] == '\0')//这里的结束条件最容易出错,关键也在这里
return ;
}
else
{
temp = &treenodes[p++];
temp->cnt = ;
for(int i = ; i < ; i++)
{
temp->next[i] = NULL;
}
cur->next[t] = temp;
}
cur = cur->next[t];
//printf("%d %c\n",cur->cnt,word[i]);
i++;
}
cur->cnt = ;
//printf("%d %c\n",cur->cnt,*word);
return ;
} int main(void)
{
int t,n,ans,last;
char phonenum[];
scanf("%d",&t);
while(t--)
{
p = ;
scanf("%d",&n);
root = &treenodes[p++];
root->cnt = ;
for(int i = ; i < ; i++)
{
root->next[i] = NULL;
}
last = ;
for(int i = ; i < n; i++)
{
scanf("%s",phonenum);
if(last)//如果只前的电话号码已经冲突,則不再插入新节点~
ans = inserts(phonenum);
if(ans == )
last = ;
}
if(last)
printf("YES\n");
else
printf("NO\n");
}
return ;
}

hdu 1671&& poj 3630 (trie 树应用)的更多相关文章

  1. POJ 3630 trie树

    Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26559 Accepted: 8000 Descripti ...

  2. hdu 1671 Phone List (Trie树)

    简单的字典树应用,在建树的时候判断就行了. 需要注意的语法: 在使用malloc和free来处理动态内存的时候,仅仅是释放了这个对象所占的内存,而不会调用这个对象的析构函数:使用new和delete就 ...

  3. hdu 1671 Phone List 字典树

    // hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...

  4. poj 2945 trie树统计字符串出现次数

    用记录附加信息的val数组记录次数即可. trie的原理:每个可能出现的字目给一个编号c,那么整个树就是一个c叉树 ch[u][c]表示 节点u走c边过去之后的节点 PS:trie树还有种动态写法,使 ...

  5. HDU 4757 可持久化trie树

    首先如果给定一些数,询问这些数中哪个数^给定的数的值最大的话,我们可以建立一颗trie树,根连接的两条边分别为0,1,表示二进制下第15位,那么我们可以建立一颗trie树,每一条从根到叶子节点的链表示 ...

  6. HDU - 1251 统计难题(trie树)

    Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).  Input输入数据的第一部 ...

  7. HDU 4760 Good Firewall ( Trie树 )

    一开始看的时候就想歪了,比赛的时候一直在YY线段树区间覆盖,然后纠结节点数太多开不下怎么办啊啊啊啊…… 然后昨天吃饭的时候也在纠结这到底是个啥题,后来发现公共前缀->前缀??!!!!->这 ...

  8. POJ 2945 trie树

    Find the Clones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7704 Accepted: 2879 Descr ...

  9. POJ 2513 trie树+并查集判断无向图的欧拉路

    生无可恋 查RE查了一个多小时.. 原因是我N define的是250500 应该是500500!!!!!!!!! 身败名裂,已无颜面对众人.. 吐槽完了 我们来说思路... 思路: 判有向图能否形成 ...

随机推荐

  1. python实现批量修改服务器密码

    需求:机房.线上有多台主机,为了保障安全,需要定期修改密码.若手动修改,费时费力易出错. 程序应该满足如下需求 : 1.在现有的excel密码表格,在最后一个字段后面生成新的密码,另存为一个新的exc ...

  2. Appscan standard怎么设置外部浏览器为IE

    Appscan standard怎么设置外部浏览器为IE   方法/步骤     首先,打开一个的Appscan 的界面中,点击菜单中的 工具 的选项   点击了工具的选项之后,弹出了下拉菜单选中为 ...

  3. Spark运行架构详解

    原文引自:http://www.cnblogs.com/shishanyuan/p/4721326.html 1. Spark运行架构 1.1 术语定义 lApplication:Spark Appl ...

  4. webpack 打包生成的index 路径引用不对

    webpack 在打包时在访问打包里面的index时,出现路径错误 修改方法为 解决方法:在config下面的index.js把 assetsPublicPath: '/', 修改为: assetsP ...

  5. sql语句怎么看效率?

    1.数据库设计当面: 对查询进行优化,应该尽量避免全表扫描,首先应考虑在where及order by设计的列上加索引. d.索引并不是越多越好,索引可以提高查询效率,同时降低了insert和updat ...

  6. [转]WPF中的ControlTemplate(控件模板)

    WPF中的ControlTemplate(控件模板)                                                                           ...

  7. Makefile知识点

    1.标准Makefile文件示例如下: #把.o .C .cxx .cpp .hxx .h添加到后缀列表中. .SUFFIXES: .o .C .cxx .cpp .hxx .h #设置变量并赋值,方 ...

  8. IT外包概要

    IT外包 前两天和朋友聊起这个外包的问题,就顺便给他说了一下,自己也整理了一下,发出来,方便更多的人. 如果有说的不准确的地方欢迎大家补充分享. 大致分两种: 项目外包, 人力外包. 简而言之:项目外 ...

  9. OpenCV cvReleaseImage把图像怎么样了?

         cvReleaseImage(img)和free.delete相同,只是把该指针img所指的内存给释放掉,但并没有把img指针本身干掉,其地址仍然不变(非NULL),只是该地址对应的内存是垃 ...

  10. css3之文本和颜色功能之text-shadow

    总本看一下 1.text-shadow 语法:text-shadow: h-shadow v-shadow blur color; h-shadow: 必需.水平阴影的位置.允许负值. v-shado ...