(NYoj 163)Phone List -- 字典树(水题)

2024-02-05 02:48
文章标签 list 163 字典 水题 nyoj phone

本文主要是介绍(NYoj 163)Phone List -- 字典树(水题),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Phone List
时间限制:1000 ms | 内存限制:65535 KB
难度:4
描述
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:

Emergency 911
Alice 97 625 999
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.

输入
The first line of input gives a single integer, 1 ≤ t ≤ 10, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 ≤ n ≤ 100000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.
输出
For each test case, output “YES” if the list is consistent, or “NO” otherwise.
样例输入
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
样例输出
NO
YES

分析:
就是一个字典树的建树问题。不懂的可以查看相关字典树的知识点。

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream> 
using namespace std;
struct node{node *next[10];int end;node(){  //构造函数,方便初始化数据 memset(next,NULL,sizeof(next));end=0;  //end=0表示一般节点,end=1标志一个电话号的结束 }
};
node *root;
bool insert(char *s)
{int i,k,flag;node *p=root;for(flag=i=0;s[i];++i){k=s[i]-'0';if(p->next[k]==NULL){p->next[k]=new node();flag=1;   //标记建立过一个新节点 } p=p->next[k];if(p->end) return 0; //碰到结束标志,则返回0 }p->end=1;  //一个电话号的结束标志 if(!flag) return 0;//字符串插入完毕未出现新建节点的操作,返回0 return 1;
}
int del(node *p)  //释放字典树空间,否则占用空间太大 
{  if(p==NULL) return 0;for(int i=0;i<10;i++)if(p->next[i]) del(p->next[i]);delete p;return 0;
} 
int main()
{int n,T;char s[11];scanf("%d",&T);while(T--){bool flag=1;root=new node();scanf("%d",&n);while(n--){scanf("%s",s);if(flag) flag=insert(s);//假如出现过混乱情况,则不再进行插入操作 }del(root);if(flag) puts("YES");else puts("NO");}return 0;
}

这篇关于(NYoj 163)Phone List -- 字典树(水题)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/679548

相关文章

一道经典Python程序样例带你飞速掌握Python的字典和列表

Python中的列表(list)和字典(dict)是两种常用的数据结构,它们在数据组织和存储方面有很大的不同。 列表(List) 列表是Python中的一种有序集合,可以随时添加和删除其中的元素。列表中的元素可以是任何数据类型,包括数字、字符串、其他列表等。列表使用方括号[]表示,元素之间用逗号,分隔。 定义和使用 # 定义一个列表 fruits = ['apple', 'banana

-bash: /bin/mv: Argument list too long mv

把labels下的所有文件mv到img文件夹下: mv labels/* img/ 报错: -bash: /bin/mv: Argument list too long  mv # Using find ... -exec + find folder2 -name '*.*' -exec mv --target-directory=folder '{}' +   # Using xar

直接得到Json串,转换为字典

0.新创建一个json文件,把json串拷贝到里面 1.先通过MainBundle找到资源对应的路径 2.将文件转换为NSData 3.通过NSJSonSerization得到字典 NSString*fileName=[[NSBundle mainBundle] pathForResource:@"myJson" ofType:@"json"];           NS

Java零基础-集合:List

哈喽,各位小伙伴们,你们好呀,我是喵手。运营社区:C站/掘金/腾讯云;欢迎大家常来逛逛   今天我要给大家分享一些自己日常学习到的一些知识点,并以文字的形式跟大家一起交流,互相学习,一个人虽可以走的更快,但一群人可以走的更远。   我是一名后端开发爱好者,工作日常接触到最多的就是Java语言啦,所以我都尽量抽业余时间把自己所学到所会的,通过文章的形式进行输出,希望以这种方式帮助到更多的初

CSS列表属性:list-style系列属性详解

CSS(层叠样式表)是用于控制网页样式的一种语言,它允许开发者以一种非常灵活的方式来设置网页元素的外观。在CSS中,list-style属性族是专门用来设置列表样式的。列表是网页设计中常见的元素,它们可以是有序列表(<ol>)或无序列表(<ul>)。list-style系列属性允许你自定义列表项前的标记,包括类型、位置和图像。 1. list-style-type list-style-typ

玩转Web之Json(四)---json与(Object/List/Map)的相互转化

在做web应用时,经常需要将json转化成Object/list/map或者将Object/List/map转化成json,通过简单封装可以在写代码是减轻很多负担。本文将给出json转化的一系列方法。 闲话不 多说,直接上代码: 先是Object /List /Map转化为Json /* 功能 :将一个对象转成json数组* 参数 :object对象* retu

【C++11 之新增容器 array、foward_list、tuple、unordered_(multi)map/set】应知应会

C++11 标准中新增了多个容器,这些容器为 C++ 程序员提供了更多的选择,以满足不同的编程需求。以下是对这些新容器的介绍和使用案例: std::array 介绍: std::array 是一个固定大小的数组容器,它在栈上分配内存,并提供了类似于标准库容器的接口。它提供了更好的类型安全性和范围检查,同时保持了与原生数组相似的性能。std::array 的大小必须在编译时确定,并且不能更改。

IOS Swift 从入门到精通:数组,集合,元组,对比,字典,枚举

目录 数组 集合 元组 Arrays vs sets vs tuples 字典  字典默认值 创建空集合 枚举 枚举关联值 枚举原始值 复杂类型:总结 数组 数组是存储为单个值的值的集合。例如,John、Paul、George 和 Ringo 是姓名,但数组可让您将它们分组为单个值,即 The Beatles。 在代码中我们这样写: let john

Initializer_list

1、定义template<class T> class initializer_list2、用途此类型用于访问初始化表中的元素。初始化表是由一系列的const T组成的表。如:auto il = { 10, 20, 30}; // 以逗号分隔,包含在一堆花括号({})内3、如要使用initializer_list,需包含头文件<initializer_list>。 4、初始化表中的

ajax+json+Struts2实现list传递(转载)

一、首先需要下载JSON依赖的jar包。它主要是依赖如下:       json-lib-2.2.2-jdk15       ezmorph-1.0.4       commons-logging-1.0.4       commons-lang-2.4       commons-collections-3.2.1       commons-beanutils      二、