Phone List

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
 
描述

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.

 
输入
The first line of input gives a single integer, 1 ≤ t ≤ 10, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 ≤ n ≤ 100000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.
输出
For each test case, output "YES" if the list is consistent, or "NO" otherwise.
样例输入
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
样例输出
NO
YES
解题思路:刚开始直接看测试数据差点误解,以为是按顺序存储的就输出yes呢。。。。仔细翻译才明白。
  
字典树,就是按照一定的顺序来构造一棵树,对于很多字符串来说,有很多重复的前缀,那么相同的前缀就不用都存储一遍了,用一个相同前缀来存储就可以。
  此题判断是否有前缀有两种情况(每次都是按位存),① 如果挨着往里存储,当此时的字符串a都存储完的时候还没有建立新的节点,说明在此之前已经有一个字符串b了,所以a是b的前缀。
  ② 当这个a字符串还没有存储完成就碰到endd=1,说明之前有一个字符串b已经结束了,b是这个a的前缀。
代码:
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; typedef struct tree{
int endd;
tree *next[];
tree(){
endd=;
memset(next,NULL,sizeof(next));
}
};
tree *root; int inser(char a[]){
int flag=;
tree *p=root;
int k;
int len=strlen(a);
for(int i=;i<len;i++){
k=a[i]-'';
if(p->next[k]==NULL){
p->next[k]=new tree;
flag=;
}
p=p->next[k];
if(p->endd){
return ;
}
}
p->endd=;
if(!flag){
return ;
}
return ;
} int main()
{
int t;
char a[];
scanf("%d",&t);
while(t--){
int n;
int flag=;
root=new tree();
scanf("%d",&n);
while(n--){
scanf("%s",a);
if(flag){
flag=inser(a);
}
}
if(!flag){
printf("NO\n");
}else{
printf("YES\n");
}
}
return ;
}

nyoj163_Phone List_字典树的更多相关文章

  1. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  2. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  3. 字典树+博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

  4. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  5. 山东第一届省赛1001 Phone Number(字典树)

    Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We know that if a phone numb ...

  6. 字典树 - A Poet Computer

    The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...

  7. trie字典树详解及应用

    原文链接    http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用   一.知识简介        ...

  8. HDU1671 字典树

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. *HDU1251 字典树

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

随机推荐

  1. vim中大小写转换

    转自:http://www.cnblogs.com/fortran/archive/2010/07/25/1784513.html vim中大小写转化的命令是:gu或者gU,形象一点的解释就是小u意味 ...

  2. Java通过JNI调用C

    Java调用C有多种方式,本文介绍笔者最近的学习过程,避免今后再犯类似的错误. 首先,Java肯定是调用C的动态链接库,即通过编译器编译后的dll/so文件. 下面介绍gcc编译dll的方法. 一般情 ...

  3. SQL like 模糊查询

    SQL 模糊查询 在进行数据库查询时,有完整查询和模糊查询之分. 一般模糊查询语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: ...

  4. spring缓存Ehcache(入门2)源码解读

    Ehcache缓存: 解读: Ehcache缓存是在继承spring缓存核心类CacheManager的基础上实现的. 常用类: EhCacheCacheManager:继承自CacheManager ...

  5. javascript 2048游戏

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 清北学堂模拟day4 捡金币

    [问题描述]小空正在玩一个叫做捡金币的游戏.游戏在一个被划分成 n行 n列的网格状场地中进行.每一个格子中都放着若干金币,并且金币的数量会随着时间而不断变化. 小空的任务就是在网格中移动,拾取尽量多的 ...

  7. Google Protocol Buffer 简单介绍

    以下内容主要整理自官方文档. 为什么使用 Protocol Buffers .proto文件 Protocol Buffers 语法 编译.proto文件 Protocol Buffers API 枚 ...

  8. jstl c标签

    判断List是否为空的一种方法是使用jstl的c标签. <c:if test="${not empty cpInfo.cpCredentials}"> </c:i ...

  9. wordpress自动清理评论回收站

    有时wordpress的垃圾评论实在让人心烦,杂草难除根,footprint吹又生.如果你有心情的话会一个个把垃圾评论放入回收站,但是时间一长,回收站里的东西越堆越多,你可以点击回收站,然后再点一下e ...

  10. 【C语言入门教程】3.1 程序的 3 种基本结构

    程序设计是一个问题求解的过程,解决问题的步骤可看作是程序的控制结构.简单地说,程序的运行过程就是数据输入.数据处理.数据输出 3 个步骤.其中,数据处理过程是否快捷和准确,主要依赖于程序控制结构的设计 ...