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

这个题目注意下要释放内存,同时他不是按顺序的。在后面也会出现有前面单词前缀的单词。

借这个题目贴下字典树的模板把
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
#include<algorithm>
using namespace std;
typedef long long ll;
struct Tire{int num;bool flag;Tire *next[10];Tire() {num = 0;flag = false;for(int i=0;i<10;i++){next[i] = NULL;}}
};
Tire *tree;
bool flag;  //判断字典树中是否有单词是别人的前缀
ll ans = 0; //记录不同的单词总数
void Insert(string word){ //在字典树中插入word字符串Tire* p = tree;for(int i=0;i<word.length();i++){int t = word[i] - '0';if( p -> next[t] == NULL ){p -> next[t] = new Tire;}p = p -> next[t];if( p -> flag ) {flag = false;}p -> num ++;}if( !p->flag ) {p -> flag = true;ans ++;}if( p -> flag ) {if( p -> num > 1 ) {flag = false;}}
}
int Find(string word){ //计算当前前缀出现次数Tire* p = tree;for(int i=0;i<word.length();i++){int t = word[i] - 'a';if( p -> next[t] == NULL ) {return 0;}p = p -> next[t];}return p -> num;
}
bool isLife(string word){//判断当前字符串在字典树中是否存在Tire* p = tree;for(int i=0;i<word.length();i++){int t = word[i] - 'a';if( p -> next[t] == NULL ) {return false;}p = p -> next[t];}if( p -> flag == true ){return true;}return false;
}
int deal(Tire* T)//释放内存空间
{int i;for(i=0;i<=9;i++){if(T->next[i]!=NULL)deal(T->next[i]);}free(T);return 0;
}
int main(){int T;cin >> T;while( T -- ) {int n;cin >> n;flag = true;tree = new Tire;while( n -- ) {string s;cin >> s;Insert(s);}if( flag ) {cout << "YES" << endl;}else cout << "NO" << endl;deal(tree);}return 0;
}

转载于:https://www.cnblogs.com/l609929321/p/8336044.html

hdu 1671 Phone List 字典树模板相关推荐

  1. [ACM] hdu 1671 Phone List (字典树)

    [ACM] hdu 1671 Phone List (字典树) Phone List Problem Description Given a list of phone numbers, determ ...

  2. HDU 1251 统计难题 字典树/STL

    统计难题 Time Limit:2000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Description Ig ...

  3. HDU - 4825 Xor Sum(字典树)

    题目链接:点击查看 题目大意:给出n个数组成的数组,再给出m个询问,每次询问给出一个x,要求从数组中选出一个k,使得k^x的值最大 题目分析:字典树求异或值最大的模板题,对于n个数直接insert到字 ...

  4. HDU - 1251 统计难题(字典树)

    题目链接:点击查看 题目大意:给出一些单词,后续再给出一些前缀,询问包含此前缀的单词一共有多少个 题目分析:这个题目的数据可能有点水,而且时间给的也很足,给了两秒,而且加上是hdu的,可以用无序map ...

  5. hdu 1251 统计难题 (字典树入门题)

    1 /******************************************************* 2 题目: 统计难题 (hdu 1251) 3 链接: http://acm.hd ...

  6. hdu 2665 Kth number(划分树模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=2665 [ poj 2104 2761 ]  改变一下输入就可以过 http://poj.org/problem? ...

  7. 字典树基本介绍(字典树模板)

    转载自:http://www.cnblogs.com/tanky_woo/archive/2010/09/24/1833717.html 字典树(讲解+模版) 又称单词查找树,Trie树,是一种树形结 ...

  8. HDU 5536 Chip Factory 字典树+贪心

    给你n个数,a1....an,求(ai+aj)^ak最大的值,i不等于j不等于k 思路:先建字典树,暴力i,j每次删除他们,然后贪心找k,再恢复i,j,每次和答案取较大的,就是答案,有关异或的貌似很多 ...

  9. hdu 5384 Danganronpa(字典树)

    题意: f(A,B)表示:B在A中作为子串出现的次数. 题目给出n个证据,m个子弹 Ai是证据.Bi是子弹.题目问:全部Bi对每一个Ai造成的伤害是多少,即每一个Bi在Ai中出现的次数总和. 解析: ...

最新文章

  1. Spark 分布式计算原理
  2. ProxyError: Conda cannot proceed due to an error in your proxy configuration
  3. oracle insert忽略重复数据,Oracle’INSERT ALL’忽略重复项
  4. 腾讯云智能制造首次披露三大战略布局,发布“511”生态伙伴计划
  5. panel内嵌程序窗体
  6. 在Qt中用默认程序打开文件
  7. SAP报表的性能优化SAP报表的性能优化
  8. mysql_rollback_MySQL的rollback--事务回滚
  9. 大智慧数据文件python_马克的Python学习笔记#模块和包 3
  10. 系统仿真基础与计算机实现,计算机综合仿真实验系统的研究与开发
  11. HDFS High Availability体系介绍(Using the Quorum Journal Manager)
  12. 内存双通道不兼容举例以及解决方案.
  13. 创建新的swap文件
  14. 【转】JVM 架构解读
  15. MySQL|Aborted connection 日志分析
  16. Katana 项目入门
  17. android 视频截屏代码,android视频截屏手机录屏实现代码
  18. widows上安装golang
  19. Linux IO体系、零拷贝和虚拟内存关系的重新思考
  20. 测试管理工具-禅道---软件缺陷和软件缺陷种类

热门文章

  1. 解决企业人力短缺难题?且看RPA如何大展身手!
  2. HDU5002:Tree(LCT)
  3. 【修真院WEB小课堂】定时器有哪些用法?
  4. 0039-如何使用Python Impyla客户端连接Hive和Impala
  5. 解读现代存储系统背后的经典算法
  6. 每个电脑都会自己的SID号和GUID号,而且不会相同。。
  7. [破解] DRM-内容数据版权加密保护技术学习(中):License预发放实现
  8. 【cl】本地安装maven的jar包报错Artifact is already in the local repository
  9. java的类加载器ClassLoader
  10. Code Chef May Challenge 2019题解