山东第一届省赛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 ...
随机推荐
- Linux监控工具介绍系列——smem
smem工具介绍 smem是Linux系统上的一款可以生成多种内存耗用报告的命令行工具.与现有工具不一样的是smem可以报告实际使用的物理内存(PSS),这是一种更有意义的指标.可以衡量虚拟内存系统的 ...
- javascript-观察者模式
观察者模式方法 1.称之为消息机制或发布-订阅者模式 2.定义了一种依赖关系解决了主体对象与观察者之间功能的耦合 观察者方法 //将观察者放在闭包中,当页面加载就立即执行 var Observ ...
- 【Linux学习】如何了解一个陌生的命令?
如何了解一个陌生的命令? 有一些命令可以用来了解某个命令本身的情况,比如这个命令的绝对路径. $which ls which 在默认路径中搜索命令,返回该命令的绝对路径. $whereis ls wh ...
- Swift实现封装PopMenu菜单,可在屏幕任意位置弹出
效果图: 说明: 代码现已支持 Swift3 语法 使用介绍: 1.初始化位置 //frame 为整个popview相对整个屏幕的位置 箭头距离右边位置,默认15 //popMenu = SwiftP ...
- 解决Android Graphical Layout 界面效果不显示
解决Android Graphical Layout 界面效果不显示 qq463431476
- 【小白的CFD之旅】11 敲门实例【续】
主要内容: 接上文[小白的CFD之旅]10 敲门实例 2.4 Materials设置2.5 Cell Zone Conditions2.6 Boundary Conditons2.7 Dynamic ...
- Web报表工具FineReport的JS API开发(一)
很多报表软件可以利用JS接口来实现更多更复杂的功能.以FineReport为例,开放了大量的JS API给用户,根据执行JS的主体不同可以将分为三大类:FR.FS和contentWindow. 在js ...
- C语言中结构体赋值问题的讨论
今天帮师姐调一个程序的BUG,师姐的程序中有个结构体直接赋值的语句,在我印象中结构体好像是不能直接赋值的,正如数组不能直接赋值那样,我怀疑这个地方有问题,但最后证明并不是这个问题.那么就总结一下C语言 ...
- 《Invert》开发日志00:缘起
按照所有程序员的惯例,编号从0开始.本系列日志将记录一款独立游戏 发起->构思->设计->实现->完善->测试(如果需要)->上线->后期维护(如果有人玩) ...
- spring有三种启动方式
spring有三种启动方式,使用ContextLoaderServlet,ContextLoaderListener和ContextLoaderPlugIn spring3.0及以后版本中已经删除Co ...