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. eAccelerator、memcached、xcache、APC 等四个加速扩展的区别

    折腾VPS的朋友,在安装好LNMP等Web运行环境后都会选择一些缓存扩展安装以提高PHP运行速度,常被人介绍的有eAccelerator.memcached.xcache.Alternative PH ...

  2. PuTTY配置

    目录 1.作用? 2.中文问题解决 ? 3.GUI支持? 4.使用密钥对实现安全快捷的无密码登陆? 5.操作习惯(Alt+Enter全屏以及字体配置) 6.附录(sshd服务器配置) 1.作用? Pu ...

  3. spring项目部署到resin4中的无法注入问题

    碰到个奇葩事啊,一个spring的项目拿到客户现场部署到resin4中,启动后各种报无法注入bean,找不到bean的问题.出现大量下图的错误: nested exception is org.spr ...

  4. 爬虫(Java实现)

    说明: 使用了htmlparser库. 运行过程: 从某个网址开始,摘取网页中的链接,并通过广度搜索,对这些链接递归执行上述操作. 在以上过程中把网址存入数据库中.以防止搜索中出现环路. 但是,程序经 ...

  5. aa

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...

  6. jsp前3章试题分析

    /bin:存放各种平台下用于启动和停止Tomcat的脚本文件 /logs:存放Tomcat的日志文件 /webapps:web应用的发布目录 /work:Tomcat把由JSP生成的Servlet存放 ...

  7. 通过trie树单词自动补全(二)

    经常使用iciba进行单词查询, 关于他的搜索建议是通过单词前缀做的索引, 所以自己想动手实现下, 当然如果借助mysql的话,一条sql语句就能实现, 网上查询了下trie正适合做这个,所以通过C语 ...

  8. linux swap 分区那点事儿

    前言 前段时间在用程序对较大数据进行处理时,发现自己电脑原有内存不够用而经常行卡死,于是想到了利用swap分区来扩容内存的方式.现在做一个简要的总结: swap分区的概念 初试swap分区是在进入实验 ...

  9. ajax给全局变量赋值问题解决示例

    今天在做项目时,遇到了一个问题.我用的是ajax,要在 ajax里面给一个全局变量赋值,结果死活赋值不上,纠结了好半天,后来上网查了查,才 知道,ajax默认是异步请求,(当要赋值时,此时的值没有拿到 ...

  10. ionic angularJS input 相关指令 以及定时器 的使用

      <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" conte ...