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: 
1. Emergency 911 
2. Alice 97 625 999 
3. 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. 

InputThe 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.OutputFor 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 题意:判断某个字符串是全部字符串中的某个字符串的前缀,有输出YES, 反之NO
思路:利用Trie来做,可以变插入变检测,如果在插入过程中有标记节点,则之前某个字符串是该字符串的前缀,如果插入完成之后,其后有子数,则该字符串是某个字符串的前缀。
   每组Case做完后,要释放空间,再建立新树。
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
#define Max 10
typedef struct TrieNode *Trie;
struct TrieNode
{
Trie next[Max];
int cnt;
TrieNode()
{
for (int i = ; i < Max; i++) {
next[i] = NULL;
cnt = ;
}
}
}; bool Insert(Trie root, char *s)
{
Trie p = root;
for (int i = ; s[i]; i++) {
int t = s[i]-'';
if (p->next[t] == NULL)
p->next[t] = new TrieNode;
p = p->next[t];
if (p->cnt >= ) return ;
}
p->cnt++;
for (int i = ; i < Max; i++) {
if (p->next[i] != NULL)
return ;
}
return ;
} void FreeTrie(Trie root)
{
for (int i = ; i < Max; i++) {
if (root->next[i] != NULL)
FreeTrie(root->next[i]);
}
delete root;
} int main()
{
//freopen("1.txt", "r", stdin);
int T;
scanf("%d", &T);
while (T--) {
int n;
char s[][];
scanf("%d", &n);
Trie root = new TrieNode;
int flag = ;
for (int i = ; i < n; i++) {
scanf("%s", s[i]);
if (Insert(root, s[i]))
flag = ;
} if (flag) printf("NO\n");
else printf("YES\n");
FreeTrie(root);
} return ;
}
												

[hdu 1671] Phone List - Trie的更多相关文章

  1. HDU 1671 Phone List (Trie·数组实现)

    链接:http://blog.csdn.net/acvay/article/details/47089657 题意  给你一组电话号码  判断其中是否有某个电话是另一个电话的前缀 字典树的基础应用   ...

  2. hdu 1671 Phone List (Trie树)

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

  3. hdu 1671&& poj 3630 (trie 树应用)

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

  4. HDU 1671 Phone List(Trie的应用与内存释放)

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

  5. hdu 1671 Phone List 字典树

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

  6. hdu 4825 Xor Sum(trie+贪心)

    hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...

  7. HDU 1671 Phone List (Trie)

    pid=1671">Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  8. HDU 1671 (字典树统计是否有前缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...

  9. POJ 3630 , HDU 1671 Phone List - from lanshui_Yang

    这道题也是一道找前缀的问题,很自然地要用到Trie树,但是如果用动态Trie树(即用指针开辟内存)的话,虽然在HDU上可以过(可能是HDU的数据比较水),但在POJ上会TLE , 所以这道题只能用静态 ...

随机推荐

  1. jQuery学习(3)

    可以在select中设置size属性的属性值,从而让下拉列表中的选项都显示出来. <!DOCTYPE html> <html> <head> <title&g ...

  2. ListOperations

    RedisOperations<K,V> getOperations()  V index(K key, long index)  V leftPop(K key)  V leftPop( ...

  3. .dhpcd导致cpu飙升问题

    因公司有业务服务器在阿里云上面,阿里云后台报警说,“有恶意程序在挖矿”,引起了高度重视,于是我登陆服务器进行排查. 登陆云服务器:系统centos7.5 第一步使用top查看资源情况. top 可以清 ...

  4. Java IO 简记

    1.File 类: l  java.io.File类:文件和目录路径名的抽象表示形式,与平台无关 l  File 能新建.删除.重命名文件和目录,但 File 不能访问文件内容本身.如果需要访问文件内 ...

  5. mongodb与mysql的命令对比

    mongodb与mysql命令对比 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(col ...

  6. Linux syslogd

    /********************************************************************************** * Linux syslogd ...

  7. EasyDarwin+ffmpeg进行PC(摄像头+麦克风)流媒体直播服务

    上一回我们描述了用EasyDarwin+ffmpeg进行摄像机直播的过程:ffmpeg推送,EasyDarwin转发,vlc播放 实现整个RTSP直播 我们再进行一个方面的描述,那就是pc摄像头+麦克 ...

  8. 4445: [Scoi2015]小凸想跑步 半平面交

    题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=4445 题解: 设点坐标,利用叉积可以解出当p坐标为\((x_p,y_p)\)时,与边i- ...

  9. javascript自我测试题

    javascript自我测试题--14道题而已! 地址:http://perfectionkills.com/javascript-quiz/ 司徒正美的答案解析:http://www.cnblogs ...

  10. BZOJ3680:吊打XXX

    我对模拟退火的理解:https://www.cnblogs.com/AKMer/p/9580982.html 我对爬山的理解:https://www.cnblogs.com/AKMer/p/95552 ...