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. java算法面试题:设计一个快速排序。双路快速排序,简单易于理解。

    package com.swift; import java.util.ArrayList; import java.util.Collections; import java.util.Compar ...

  2. java面向对象思想2

    1.主函数是一类特殊的函数,作为程序入口,可被虚拟机调用.主函数格式是固定的.public:函数访问权限最大.static:代表函数随着类的加载已经存在.void:主函数没有具体返回值.main:不是 ...

  3. JSON.parse(text[, reviver])

    1. JSON.parse(text[, reviver])text 必需 有效的json字符串reviver 可选 函数 2. 举栗子1) 只有第一个参数 let objStr = '{" ...

  4. mysql基础,修改数据表

  5. js动态刷新时间

    <script type="text/javascript"> //取得系统当前时间 function getTime(){ var myDate = new Date ...

  6. Python知识点进阶——生成器

    生成器 为什么要将列表转化为迭代器? 因为列表太大的话用内存太大,做成迭代器可以节省空间,用的时候再拿出部分. 生成器是不会把结果保存在一个系列中,而是保存生成器的状态,在每次进行迭代时返回一个值,知 ...

  7. psutil模块的基础使用

    注:Python并没有自带psutil模块,需要自己去安装 安装psutil模块 pip install psutilorpip3 install psutil 一.导入模块 import psuti ...

  8. 权限组件(11):基于formset实现批量增加

    效果图: 增加页面: 编辑页面: 因为后面要对权限进行批量操作,所以先用这个示例演示下如何实现批量操作 数据库 from django.db import models class Menu(mode ...

  9. 4Sum II

    https://leetcode.com/submissions/detail/153740275/ class Solution { public: int fourSumCount(vector& ...

  10. TCP/IP网络编程之多播与广播

    多播 多播方式的数据传输是基于UDP完成的,因此,与UDP服务端/客户端的实现非常接近.区别在于,UDP数据传输以单一目标进行,而多播数据同时传递到加入(注册)特定组的大量主机.换言之,采用多播方式时 ...