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. 如果碰到git提示“ignored tracked with git”,那么使用以下命令解决

    命令:git rm --cached -r 文件/文件夹 问题在初始化git仓库的时候没有创建.gitignore文件来过滤不必要提交的文件, 后来却发现某些文件不需要提交, 但是这些文件已经被提交了 ...

  2. Hadoop MapReduce编程 API入门系列之MapReduce多种输出格式分析(十九)

    不多说,直接上代码. 假如这里有一份邮箱数据文件,我们期望统计邮箱出现次数并按照邮箱的类别,将这些邮箱分别输出到不同文件路径下. 代码版本1 package zhouls.bigdata.myMapR ...

  3. JavaScript操作HTML&CSS简单入门

    - Java攻城狮学习路线 - 一. JavaScript基础 输出 使用 window.alert() 弹出警告框. 使用 document.write() 方法将内容写到 HTML 文档中. 使用 ...

  4. udacity_javascript设计模式

    javascript设计模式 的学习记录 在优达学城上找到的 <javascript设计模式> 他主要是带动我们的思考 在 <第二章 分离重构> 中使用了 model octo ...

  5. 登录linux,输入ls显示anaconda-ks.cfg cobbler.ks ....., 原因在于root@ ~ / 区别

    今天登录linux测试机,想要创建目录,ls的时候,找不到之前的的目录,才发现是目录不对的问题. 首先,先要弄清楚 [root@330c353813ea ~] 和 [root@330c353813ea ...

  6. 【Arduino】基于arduino的激光坦克

    历经了总共40来个小时,终于将这个激光坦克做好了. 这是本人的第一件像样的arduino作品. 用arduino为主控制器,配有32路舵机驱动板(在这有些大材小用了),以及一个wr703n的路由器当做 ...

  7. C-Store论文阅读笔记

    C-Store论文由今年的图灵奖获得者Mike Stonebraker提出来,整体架构在数据库领域相当不错.数据库采用读写分开存的架构,只写块的数据定期会和只读块儿的数据进行合并,产生新的只读块儿.而 ...

  8. SqlServer动态变换库名

    declare @tname varchar(20),@num intset @tname='Players_Log_L10001'declare @sql Nvarchar(1000)=N'sele ...

  9. 关于dlg和pro的问题

    微软链接:http://technet.microsoft.com/zh-cn/subscriptions/bb983387.aspx CDialogEx::CDialogEx 构造 CDialogE ...

  10. eclipse tomcat发布路径在哪?