山东第一届省赛1001 Phone Number(字典树)
Phone Number
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
Given N phone numbers, your task is to find whether there exits two numbers A and B that A is B’s prefix.
输入
The first line of input in each test case contains one integer N (0<N<1001), represent the number of phone numbers.
The next line contains N integers, describing the phone numbers.
The last case is followed by a line containing one zero.
输出
示例输入
2
012
012345
2
12
012345
0
示例输出
NO
YES
提示
来源
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int Max = + ;
struct Node
{
int value;
int cnt;
Node * Next[];
};
Node * root;
char str[Max];
bool build(char * s)
{
Node * p = root, *temp;
int len = strlen(s);
for (int i = ; i < len; i++)
{
int id = str[i] - '';
if (p->Next[id] == NULL)
{
temp = new Node;
temp->value = id;
temp->cnt = ;
for (int i = ; i < ; i++)
temp->Next[i] = NULL;
p->Next[id] = temp;
}
p = p->Next[id];
if (p->cnt)
return false;
}
return true;
}
void destroy(Node * temp)
{
if (temp == NULL)
return;
for (int i = ; i < ; i++)
destroy(temp->Next[i]);
delete temp;
}
int main()
{
int n;
while (scanf("%d", &n) != EOF && n)
{
root = new Node;
for (int i = ; i < ; i++)
root->Next[i] = NULL;
bool flag = true;
while (n--)
{
scanf("%s", str);
if (!flag)
continue;
if (!build(str))
flag = false;
}
if (flag)
printf("YES\n");
else
printf("NO\n");
destroy(root);
}
return ;
}
山东第一届省赛1001 Phone Number(字典树)的更多相关文章
- Greatest Number 山东省第一届省赛
Greatest Number Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Saya likes math, because ...
- Sdut 2151 Phone Numbers (山东省ACM第一届省赛题 A)
题目描述 We know thatif a phone number A is another phone number B's prefix, B is not able to becalled. ...
- 湖南省第六届省赛题 Biggest Number (dfs+bfs,好题)
Biggest Number 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 You have a maze with obstacles and non-zero di ...
- ACM Sdut 2158 Hello World!(数学题,排序) (山东省ACM第一届省赛C题)
题目描述 We know thatIvan gives Saya three problems to solve (Problem F), and this is the firstproblem. ...
- 2019 上海网络赛 F Rhyme scheme (字典树DP)
题目:https://nanti.jisuanke.com/t/41414 题意:求长度为n的第k个bell number , 就是第i位的选取范围在 1-(i-1)位的最大值 +1,第一位固定为 ...
- 第一届山东省ACM——Phone Number(java)
Description We know that if a phone number A is another phone number B’s prefix, B is not able to be ...
- 第一届山东省ACM——Balloons(java)
Description Both Saya and Kudo like balloons. One day, they heard that in the central park, there wi ...
- HDU 4759 Poker Shuffle(2013长春网络赛1001题)
Poker Shuffle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- 中南大学第一届长沙地区程序设计邀请赛 New Sorting Algorithm
1352: New Sorting Algorithm Time Limit: 1 Sec Memory Limit: 128 MB Description We are trying to use ...
随机推荐
- ORACLE查看SQL的执行次数/频率
在ORACLE数据库应用调优中,一个SQL的执行次数/频率也是常常需要关注的,因为某个SQL执行太频繁,要么是由于应用设计有缺陷,需要在业务逻辑上做出优化处理,要么是业务特殊性所导致.如果执行频繁的S ...
- Nagios学习实践系列——配置研究[监控当前服务器]
其实上篇Nagios学习实践系列——基本安装篇只是安装了Nagios基本组件,虽然能够打开主页,但是如果不配置相关配置文件文件,那么左边菜单很多页面都打不开,相当于只是一个空壳子.接下来,我们来学习研 ...
- SQL SERVER 中如何用脚本管理作业
在SQL SERVER中用脚本管理作业,在绝大部分场景下,脚本都比UI界面管理作业要高效.简洁.打个简单的比方,如果你要查看作业的运行时长,如果用UI界面查看,100个作业,你就得在历史记录里面至少查 ...
- CSS white-space 属性
实例 规定段落中的文本不进行换行: p { white-space: nowrap } nowrap:文本不会换行,文本会在在同一行上继续,直到遇到 <br> 标签为止.
- Spark调度管理(读书笔记)
Spark调度管理(读书笔记) 转载请注明出处:http://www.cnblogs.com/BYRans/ Spark调度管理 本文主要介绍在单个任务内Spark的调度管理,Spark调度相关概念如 ...
- SCCM 2012 R2安装部署过程和问题(三)
上篇 SCCM 2012 R2安装部署过程和问题(二) 个人认为对于使用SCCM 2012的最重要的经验是耐心. SCCM采用分布式部署的架构,不同的站点角色可以部署在不同的服务器上,站点角色之间的通 ...
- Json解析工具Jackson(简单应用)
原文http://blog.csdn.net/nomousewch/article/details/8955796 概述 Jackson库(http://jackson.codehaus.org),是 ...
- strcat 函数的实现
原型 extern char *strcat(char *dest,char *src); 用法 #include <string.h> 功能 把 ...
- Ubuntu 网络参数设置
修改网络设置 sudo vim /etc/network/interfaces 修改计算机名 sudo vim /etc/hostname sudo vim /etc/hosts
- [WPF系列]-ListBox
引言 本文就WPF中的ListBox常用项给以实例代码演示,包括隐蔽属性的设置,Style设置,以及ControlTemplate的自定义. Listbox平滑滚动 <ListBox Ite ...