Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit
Status

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<stdio.h>
#include<string.h>
char str[100000+10][50];
int ch[100010][50];
int word[100010];
int sz;
void init()
{
sz=1;
memset(ch[0],0,sizeof(ch[0]));
memset(word,0,sizeof(word));
}
void insert(char *s)
{
int l=strlen(s);
int u=0;
for(int i=0;i<l;i++)
{
int c=s[i]-'0';
if(!ch[u][c])
{
memset(ch[sz],0,sizeof(ch[sz]));
ch[u][c]=sz;
sz++;
}
u=ch[u][c];
word[u]++;
}
}
int find(char *s)
{
int l=strlen(s);
int u=0;
for(int i=0;i<l;i++)
{
int c=s[i]-'0';
u=ch[u][c];
if(word[u]==1)
return 1;
}
return 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",str[i]);
insert(str[i]);
}
int flog=1;
for(int i=0;i<n;i++)
{
if(!find(str[i]))
{
flog=0;
break;
}
}
if(flog)
printf("YES\n");
else printf("NO\n");
}
return 0;
}

poj--3630--Phone List(字典树+前缀判断)的更多相关文章

  1. poj 3630 Phone List(字典树)

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

  2. Phone List POJ 3630 Trie Tree 字典树

    Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29416   Accepted: 8774 Descr ...

  3. POJ 2001 Shortest Prefixes(字典树)

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

  4. 9-11-Trie树/字典树/前缀树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第9章  查找 - Trie树/字典树/前缀树(键树) ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接☛☛☛ <数据结构-C语言版>(严蔚 ...

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

  6. poj 1056 IMMEDIATE DECODABILITY 字典树

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

  7. TRIE 字典树 前缀紧急集合!

    TRIE: 在计算机科学中,Trie,又称前缀树或字典树,是一种有序树状的数据结构,用于保存关联数组,其中的键通常是字符串.——百度百科 自我理解: trie树,是一种处理字符串前缀的数据结构,通常会 ...

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

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

  9. poj 1204 Word Puzzles(字典树)

    题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意 ...

随机推荐

  1. c语言return与exit的区别

    2013-09-0918:54:33 exit函数在头文件stdlib.h中,函数原型: void exit(int status); exit(0) 正常运行程序并退出程序. exit(1) 非正常 ...

  2. react拼接class&将JS标签转换为HTML

    1.在JS中混杂字符和HTML标签,识别方法: const menuList = ['门店', '星享俱乐部', '菜单', '<hr></hr>', '星巴克移动应用', ' ...

  3. 第一天:java与mysql的连接工具类

    第一天:java与mysql的连接工具类 java最新版马上就要收费,这无疑是这门语言的衰败起始,毕竟在中国收费便难发展,例如c#,但是毕业设计已经选好用java来写一个动态网站, 这已经是一个事实, ...

  4. javascript中异常处理案例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 关于电脑安装新硬盘,出现无法是识别设备,03F0问题解答。

    问题说明:在添加新的硬盘,切确定硬盘没有坏的情况下,无法识别出新的硬盘. 解决方案: 1.检查bios系统里的安全模式,是否处于开启中.因为在windows 8.1以上的版本中,不开启的情况下只能读取 ...

  6. 高级I/O函数

    给套接口上的I/O设置超时 1.调用alarm,在调用超过指定时间时产生SIGALARM信号,这涉及到信号处理,而且可能和进程中其他的alarm冲突 2.使用select阻塞在等待I/O上,selec ...

  7. 【从零开始】【Java】【2】项目最开始都有什么鬼

    闲聊 刨其根知其底. 让我们从一开始就慢嚼细咽. 开始 先来看下项目都有什么: 项目结构图 pom文件图 项目结构 项目=核心代码+依赖管理文件+说明文件+IDE配套文件+外部依赖包: 核心代码:sr ...

  8. 场景报错Error -27492: "HttpSendRequest" failed, Windows error code=12029 (cannot connect) and retry limit (0) exceeded for URL=""

    1.现象:loadrunner场景执行,tps图是一段很平稳,然后直线触底,一段时间,直线恢复平稳,触底这段时间报错信息如下: Action.c(6): Error -27492: "Htt ...

  9. HTML5 Canvas绘制的下雪效果

    在HTML页面的HEAD区域直接引入snow.js即可,如下:<script type="text/javascript" src="js/snow.js" ...

  10. XSS Chanllenges 6-10

    Stage #6 测试代码</xss> 存在过滤,并且也没有其他输入点,尝试构建" onmousemove="alert(document.domain),并查看源代码 ...