Phone List

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description
Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let’s say the phone catalogue listed these numbers:
1. Emergency 911
2. Alice 97 625 999
3. Bob 91 12 54 26
In this case, it’s not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob’s phone number. So this list would not be consistent.
 
Input
The first line of input gives a single integer, 1 <= t <= 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 <= n <= 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.
 
Output
For each test case, output “YES” if the list is consistent, or “NO” otherwise.
 
Sample Input
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
 
Sample Output
NO
YES
 
 
思路:经典字典树(Trie)应用,只不过这题一开始做的时候是WA的,因为自己默认号码输入顺序是按照长度递增的。。。。后来才发现。。。

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=; struct node{
int m;
int v;
node *next[];
node()
{
for(int i=;i<;i++)
next[i]=NULL;
m=;
}
}; node *root;
int n;
bool flag; void insert_tree(char* str)
{
int len=strlen(str);
if(root==NULL)
root=new node;
node *p=root,*q;
for(int i=;i<len;i++)
{
if(p->m==)
{
flag=true;
return; //the end
}
int id=str[i]-'';
if(p->next[id]==NULL)
{
q=new node;
p->next[id]=q;
p=p->next[id];
}
else
{
p=p->next[id];
if(i==len-)
{
flag=true;
return; //the end;
}
}
if(i==len-)
{
p->m=;
}
}
} void del_tree(node* rt)
{
for(int i=;i<;i++)
{
if(rt->next[i]!=NULL)
del_tree(rt->next[i]);
}
delete rt;
} int main()
{
int t;
char str[maxn];
scanf("%d",&t);
while(t--)
{
flag=false;
root=NULL;
scanf("%d",&n);
while(n--)
{
scanf("%s",str);
if(flag)
continue;
else
insert_tree(str);
}
if(flag)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
del_tree(root);
}
return ;
}
 
代码:
 

HDOJ-1671 Phone List的更多相关文章

  1. 字典树trie的学习与练习题

    博客详解: http://www.cnblogs.com/huangxincheng/archive/2012/11/25/2788268.html http://eriol.iteye.com/bl ...

  2. <转Tanky Woo> 字典树

    又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:利用字符串的公共前缀 ...

  3. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  5. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  6. HDOJ 1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  7. hdoj 1385Minimum Transport Cost

    卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...

  8. HDOJ(2056)&HDOJ(1086)

    Rectangles    HDOJ(2056) http://acm.hdu.edu.cn/showproblem.php?pid=2056 题目描述:给2条线段,分别构成2个矩形,求2个矩形相交面 ...

  9. 继续node爬虫 — 百行代码自制自动AC机器人日解千题攻占HDOJ

    前言 不说话,先猛戳 Ranklist 看我排名. 这是用 node 自动刷题大概半天的 "战绩",本文就来为大家简单讲解下如何用 node 做一个 "自动AC机&quo ...

  10. 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design

    题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...

随机推荐

  1. 第31题:LeetCode946. Validate Stack Sequences验证栈的序列

    题目 给定 pushed 和 popped 两个序列,只有当它们可能是在最初空栈上进行的推入 push 和弹出 pop 操作序列的结果时,返回 true:否则,返回 false . 示例 1: 输入: ...

  2. Mysql之1451 - Cannot delete or update a parent row: a foreign key constraint fails...解决办法记录

    今天使用delete语句删除一张表中的一条信息时,提示了这么一个错误:1451 - Cannot delete or update a parent row: a foreign key constr ...

  3. Mysql数据库的权限、索引基本操作

    数据库的关闭方法: .优雅的关闭数据库的方法: mysqladmin -uroot -p123456 shutdown .脚本关闭: /etc/init.d/mysqld stop .使用kill信号 ...

  4. Vue2+webpack+node 配置+入门+详解

    Vue2介绍 1.vue2.0 Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架. Vue 的核心库只关注视图层 采用单文件组件 复杂大型单页应用程序(SPA) 响 ...

  5. emwin如何在windows10下vs2015或2017进行仿真。

    Make sure the selected Windows SDK is installed:Properties -> Configuration Properties -> Gene ...

  6. C# SQL数据库学习时遇到到一些异常

    1一些关于SqlHelper类的异常: DataAdapter.SqlHelper”的类型初始值设定项引发异常. ExecuteNonQuery.SqlHelper”的类型初始值设定项引发异常. Ex ...

  7. v-model 的修饰符

    1..trim 自动过滤输入内容最开始 和 最后的 空格,中间的会保留一个空格,多的会被过滤掉 2..lazy 一般情况下,在input的 v-model是一直在同步 输入的内容与显示的内容,不过再添 ...

  8. POJ 2771 Guardian of Decency (二分图最大点独立集)

    Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6133   Accepted: 25 ...

  9. loj6387 「THUPC2018」绿绿与串串 / String

    还是很好做的,大致就是manacher,每个位置为中心的最长回文串要是能抵到最右边就合法,要是能抵到最左边,那这个点的是否合法取决于以这个点为中心的最长回文串的右端点是否合法. #include &l ...

  10. loj2057 「TJOI / HEOI2016」游戏

    记横联通是一块横着的没有硬石头的地,把他们编号.竖联通同理. 对于一个空地,将其横联通编号和竖联通编号连边,二分图匹配,最大匹配为答案. #include <iostream> #incl ...