hdu Phone List
Problem 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:
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.
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
算法:字典树
题意:给你一些单词让你判断这单词中,如果有单词作为另一个的前缀就输出NO;否则YES
代码:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
using namespace std;
#define Max 20
struct dot
{
dot *next[Max];
int flag;
};
dot *newnode()
{
dot *temp=new dot;
int i;
temp->flag=0;
for(i=0;i<Max;i++)
temp->next[i]=NULL;
return temp;
}
void tree(char *st,dot *root,int &k)
{
int len=strlen(st);
dot *p=root;
int id=0;
for(int i=0;i<len;i++)
{
id=st[i]-'0'; if(p->next[id]==NULL)
p->next[id]=newnode();
if(p->flag)//例如之前出现单词abc,现在是abcd在这判断可以判断出
{
k=1;
return ;
}
p=p->next[id];
}
p->flag=1;
if(p->next[id]!=NULL)//例如之前出现abcd,现在是abc这在这判断出来
{
k=1;return ;
}
p->next[id]=newnode();
}
void del(dot *p)
{
if(p==0) return ;
for(int i=0;i<Max;i++)
if(p->next[i])
del(p->next[i]);
delete p;
}
int main()
{
int n,m,k;
char st[15];
scanf("%d",&n);
while(n--)
{ dot *root=new dot;
root=newnode();
scanf("%d",&m);
k=0;
while(m--)
{
scanf("%s",&st);
if(k==0)//如果还没出现单词为另一个单词的前缀这种情况,就继续建树
tree(st,root,k);
}
if(k) printf("NO\n");
else printf("YES\n");
del(root);
}
return 0;
}
hdu Phone List的更多相关文章
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- hdu 4329
problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟 a. p(r)= R'/i rel(r)=(1||0) R ...
- HDU 2586
http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:求最近祖先节点的权值和 思路:LCA Tarjan算法 #include <stdio.h&g ...
随机推荐
- Java语言实现简单FTP软件------>FTP软件效果图预览之上传功能(三)
下面展示一下上传功能的过程 1.上传前 上传前选择好要将文件或文件夹上传到远程FTP服务器的哪个目的目录下. 2.上传中 添加上传任务 上传任务完成进度显示 3.上传完成 ============== ...
- web标准(复习)--8
今天我们开始学习下拉及多级弹出菜单,包含以下内容和知识点: 带下拉子菜单的导航菜单 绝对定位和浮动的区别和运用 css自适应宽度滑动门菜单 一.带下拉子菜单的导航菜单下拉菜单在一些企业网站应用尤为广泛 ...
- web移动开发的小细节(持续添加)
1.触屏设备上如何去掉数字转化为拨号链接的方法 <meta name=”format-detection” content=”telephone=no” /> telephone=no就禁 ...
- JQUERY的应用
JQUERY的应用,以及和JS的对比: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...
- IIS自定义404错误页显示“系统找不到指定的文件”解决方法
在IIS站点属性里面设置了自定义的404错误页面为一个文件之后,有时一直不生效,总是提示这样一句话:“系统找不到指定的文件”. 其实这种错误也只是在某些网站程序中出现,其实解决办法很简单.这是由于II ...
- ROM包内的大致框架及各个文件的作用[转]
1.system/app这个里面主要存放的是常规的应用程序,都是以apk格式结尾的文件,在这个文件夹下面的程序为系统默认的组件,个人安装的软件不会出现在这里,而是data文件夹中. 2.system/ ...
- Codeforces 159D Palindrome pairs
http://codeforces.com/problemset/problem/159/D 题目大意: 给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数. 思路:num[i]代表左端点 ...
- 【Xamarin 开发 IOS --IOS 页面导航概念Segue】
Storyboard里面的几种Segue区别及视图的切换:push,modal,popover,replace和custom 一.视图切换类型介绍在storyboard中,segue有几种不同的类型, ...
- BOT、BT、PPP形式介绍(3)
PPP 20世纪90年代后,一种崭新的融资模式-PPP模式(Public-Private-Partnership,即“公共部门-私人企业-合作”的模式)在西方特别是欧洲流行起来,在公共基础设施 ...
- log4j 突然不打印记录,提示:No appenders could be found for logge,处理方法
log4j 一直都在使用正常,log4j.xml配置.代码都没有修改,突然不打印记录,出现下面提示: log4j:WARN No appenders could be found for logger ...