Phone List——Trie树
来源:互联网 发布:国家顶级域名有哪些 编辑:程序博客网 时间:2024/06/05 03:41
题目描述:
题目链接: HDU 1671 http://acm.hdu.edu.cn/showproblem.php?pid=1671
给出一份电话号码列表,如果不存在有一个号码是另一个号码的前缀,我们就说这份电话号码列表是合法的。让我们看看如下号码列表:
1. Emergency 911
2. Alice 97625999
3. Bob 91125426
在这组号码中,我们不能拨通 Bob 的电话,因为当你按下 Bob 电话号码的前 3 个数字“911”时,电话局会把你的拨号连接到 Emergency 的线路。
所以这组号码是不合法的。
输入格式:
有多组输入数据。
第一行输入一个正整数 t(1<=t<=40),表示数据组数。
每组数据第一行是一个正整数 n(1<=n<=10000),表示电话号码的数量。
接下来有 n 行,每行一个电话号码,每个电话号码是不超过 10 位的连续数字。
输出格式:
对每组数据,如果电话号码列表合法,则输出“YES”,不合法则输出“NO”。
样例输入:
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
样例输出:
NO
YES
题目分析:
trie树模板题。1、如果有经过一个号码的结尾,则不合法,只需在每个号码的尾巴标记一下。2、建树时如果尾部是别人的一部分,则也不合理,建时判断一下。
附代码:
#include<iostream>#include<cstring>#include<string>#include<cstdlib>#include<cstdio>#include<ctime>#include<queue>#include<cmath>#include<cctype>#include<set>#include<map>#include<iomanip>#include<algorithm>using namespace std;int n,t,tot;char s[15];bool check;struct node{ int son[15]; bool check;}trie[100010];bool create(){ int len,u=0; len=strlen(s); for(int i=0;i<len;i++) { if(trie[u].son[s[i]-'0']==0) trie[u].son[s[i]-'0']=++tot; else if(i==len-1) return true; u=trie[u].son[s[i]-'0']; if(trie[u].check==true) return true; } trie[u].check=true; return false;}int main(){ //freopen("lx.in","r",stdin); scanf("%d",&t); while(t--) { tot=0;check=false; memset(trie,0,sizeof(trie)); scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%s",s); if(create()) check=true; } if(check==true) printf("NO\n"); else printf("YES\n"); } return 0;}
阅读全文
2 0
- Phone List——Trie树
- 【Trie树】HDU 1671—— Phone List
- HDU 1671——Phone List(Trie树)
- Phone List(trie树)
- C++——字典树(Trie树)例题——Phone List(POJ3630)(HDU1671)
- poj 3630 Phone List trie树—字典树 静态数组版!!!
- Sicily 1426 Phone List(Trie树)
- POJ 3630 Phone List 【Trie树入门】
- ZOJ 2876 Phone List(trie树)
- hdu 1671 Phone List (Trie树,水题)
- hdu 1671 Phone List trie树
- Phone List(poj3630,简单trie树)
- Trie树入门题目--HDU1671 Phone List
- 1671 Phone List (Trie树)
- HDU 1671 Phone List(字典树Trie)
- POJ 3630 - Phone List (Trie树)
- HDU 1671 Phone List Trie树
- Phone List - HDU 1671 Trie树
- Jsp页面中超链接不能打开带汉字的文件的解决
- jstree的增删改
- Ajax与Jquery
- CAD图纸批量转换成PDF的常用方法
- B+ tree 删除算法
- Phone List——Trie树
- 判断点是否在不规则矩形区域opencv demo
- 静态代理、JDK与CGLIB动态代理
- JavaScript学习之路<八>
- JavaScript学习之路<九>
- Java基础
- JavaScript学习之路<十>
- Jquery 中 ajaxSubmit使用讲解
- netty学习(三)ChannelInboundHandler和ChannelOutboundHandler