Phone Number

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

We know that if a phone number A is another phone number B’s prefix, B is not able to be called. For an example, A is 123 while B is 12345, after pressing 123, we call A, and not able to call B.
Given N phone numbers, your task is to find whether there exits two numbers A and B that A is B’s prefix.
 

输入

 The input consists of several test cases.
 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.

输出

 For each test case, if there exits a phone number that cannot be called, print “NO”, otherwise print “YES” instead.

示例输入

2
012
012345
2
12
012345
0

示例输出

NO
YES

提示

 

来源

 2010年山东省第一届ACM大学生程序设计竞赛
题意:给出几个字符串问是否会有前缀产生,直接字典树搞定
 #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(字典树)的更多相关文章

  1. Greatest Number 山东省第一届省赛

    Greatest Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Saya likes math, because ...

  2. 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. ...

  3. 湖南省第六届省赛题 Biggest Number (dfs+bfs,好题)

    Biggest Number 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 You have a maze with obstacles and non-zero di ...

  4. ACM Sdut 2158 Hello World!(数学题,排序) (山东省ACM第一届省赛C题)

    题目描述 We know thatIvan gives Saya three problems to solve (Problem F), and this is the firstproblem. ...

  5. 2019 上海网络赛 F Rhyme scheme (字典树DP)

    题目:https://nanti.jisuanke.com/t/41414 题意:求长度为n的第k个bell number  ,  就是第i位的选取范围在 1-(i-1)位的最大值 +1,第一位固定为 ...

  6. 第一届山东省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 ...

  7. 第一届山东省ACM——Balloons(java)

    Description Both Saya and Kudo like balloons. One day, they heard that in the central park, there wi ...

  8. HDU 4759 Poker Shuffle(2013长春网络赛1001题)

    Poker Shuffle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  9. 中南大学第一届长沙地区程序设计邀请赛 New Sorting Algorithm

    1352: New Sorting Algorithm Time Limit: 1 Sec  Memory Limit: 128 MB Description We are trying to use ...

随机推荐

  1. CentOS 6.5 安装 MySQL5.6 并用Navicat for MySQL 连接

    环境 : MySQL 5.6.21 64位 CentOS 6.5 64位 VMware 10 Navicat for MySQL 11 1.卸载自带mysql [root@localhost  ~]# ...

  2. Linux LVM学习总结——删除卷组VG

    在Linux系统中,如何删除一个卷组(VG)呢? 下面我总结了一下如何删除卷组(VG)的具体步骤,仅供参考,如有不足,敬请指出.谢谢!在下面的例子中,我想删除卷组VolGroup05. 步骤1: 查看 ...

  3. [翻译]——SQL Server使用链接服务器的5个性能杀手

    前言: 本文是对博客http://www.dbnewsfeed.com/2012/09/08/5-performance-killers-when-working-with-linked-server ...

  4. linux ssh和scp实例

    ssh 192.160.1.100 -p 40012 scp user 192.169.72.2:/ scp -i /id_rsa -P40027 root@221.212.235.17:/sdzw/ ...

  5. 遇到shell重定向的一个奇怪问题:'消失'的标准输入!

    需求: 把找到的文件逐行输出,然后用rm在许可的情况下删除   前置准备:  $ls rm.sh test1 test2 test3 test4 test5 test6 $cat rm.sh #! / ...

  6. 我是如何把VC6一直用到2016年的

    写下这个标题的时候,也是表明必须需要改变的时候了…… 黄山松 (Tom Huang) 发表于博客园http://www.cnblogs.com/tomview/ 最早从windows3.1使用vb3编 ...

  7. Centos 6.5 安装ELK

    安装java 查看java安装路径 修改环境变量 添加如下内容: export JAVA_HOME=/usr/java/jdk1.8.0_91 export PATH=$JAVA_HOME/bin:$ ...

  8. 3. Python 简介

    3. Python 简介 下面的例子中,输入和输出分别由大于号和句号提示符 ( >>> 和 ... ) 标注:如果想重现这些例子,就要在解释器的提示符后,输入 (提示符后面的) 那些 ...

  9. CI连接多个数据库

    参考http://codeigniter.org.cn/user_guide/database/connecting.html手册 首先,在database.php中设置多个数据库的连接信息,这里命名 ...

  10. Java程序设计之合租房synchronized(二)

    一号和二号合租一间房,里面共用一个卫生间对象,这是要用到synchronized关键字,一号与二号同时使用卫生间时,一个需要wait()等待被唤醒,另外一个使用完之后卫生间对象被释放,这时候刚刚使用的 ...