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. ORACLE判别字段是否包含中文

    在ORACLE数据库中如何查找那些字段里面包含中文的数据记录呢,有时候就是有这样的特殊需求,下面整理了一些判别字段中包含中文记录的几个方法 1:使用ASCIISTR函数判别   ASCIISTR函数说 ...

  2. JavaScript(js)的replace问题的解决

    我是前端的门外汉,js我用得比较少.今天意外发现js自带的replace “居然”只替换1处,而其它的许多许多语言都是替换全部的. 你可能会说,切,我早就知道.高手请绕道. 你可能会说,用js的正则就 ...

  3. 分布式架构中一致性解决方案——Zookeeper集群搭建

    当我们的项目在不知不觉中做大了之后,各种问题就出来了,真jb头疼,比如性能,业务系统的并行计算的一致性协调问题,比如分布式架构的事务问题, 我们需要多台机器共同commit事务,经典的案例当然是银行转 ...

  4. sql server: sql script

    select ProductGUID,ProductName,ProjectGUID from dbo.Product /* F637A079-E22B-4E50-87E9-000147B1B1F4 ...

  5. AES —— JAVA中对称加密和解密

    package demo.security; import java.io.IOException; import java.io.UnsupportedEncodingException; impo ...

  6. mysql sum 和 count 函数 合并使用

    SELECT sum(start) as total, count(start) as rows FROM table where....

  7. 关于linx中man命令内容中第一行数字的含义

    我们知道linux中man这玩意特别厉害,我们要查么个命令的使用方法.如man ls 出现如下内容 关于这写数字的含义如下表格

  8. java HashMap

    HashMap 的性能因子 1. 容量:表示桶位的数量. 2. 初始容量: 表在创建是所拥有的桶位数.   如果你知道将要在HashMap存储多少项,创建一个初始容量合适的HashMap将可以避免自动 ...

  9. Java Generics and Collections-8.1

    8.1 Take Care when Calling Legacy Code 通常,泛型都是在编译时检查的,而不是运行时.便意识检查可以提早通知错误,而不至于到运行时才出问题. 但有时后编译时检查不一 ...

  10. c++中static关键字的用法总结

    类中的静态成员真是个让人爱恨交加的特性.我决定好好总结一下静态类成员的知识点,以便自己在以后面试中,在此类问题上不在被动. 静态类成员包括静态数据成员和静态函数成员两部分. 一 静态数据成员: 类体中 ...