POJ3630-Phone List-Trie字典树模板题
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.
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
- 题意:
对于每一组数据里面的每行字符串,如果存在某一个串是另外一个串的前缀关系的话,输入NO,不存在的话输出YES。
注意审题。- 注意:
该题没有给出数据范围,数组开到1e4+20会错,开到1e5+20即可。- 详情看代码注释。也可以考虑用链表指针来写。
- #include<stdio.h>
- #include<iostream>
- #include<string.h>
- using namespace std;
- const int N=1e5+;
- int ch[N][],len;//15是开的字符集大小(该题目给的是0-9),该数组用于存储trie树
- bool book[N];//==1表示从根节点到该点,所经过的边上字母组成的字符串是实际字符串集合中的元素
- char a[];//题目给出的是n个长度不超过10的字符串
- int tot;//总节点数
- //对一个字符集为数字的trie树插入一个字符串s
- bool insertt(char *s) //传入一个字符数组
- {
- // int len=strlen(s);
- int u=,flag=;
- for(int i=; i<len; i++)
- {
- int c=s[i]-'';
- if(ch[u][c]==)//表示不存在这条边,所以需要新建一个节点和一条新的边
- {
- tot++;
- ch[u][c]=tot;//tot为总结点数,新建了一个节点
- }
- else //else if(ch[u][c]!=0)
- {
- if(i==len-)//如果遍历到最后一个节点并且没有插入任何一个新节点
- flag=;//就说明存在前缀关系,返回flag=1
- }//else if(i==len-1)
- u=ch[u][c];
- if(book[u]==)//经过了某一个有过标记的节点
- flag=;
- }
- book[u]=;//一个节点连着接下去的一条边,而不是上一条边
- //bo给的赋值为真,给的是最后一个字母所连着的的一个节点
- //三个字母是有四个节点
- //给当前字符串的最后一个字母进行标记,代表字符串结尾标志
- return flag;
- }
- int main()
- {
- int t,n;
- scanf("%d",&t);
- while(t--)
- {
- scanf("%d",&n);
- tot=;//代表新建了一个根节点
- //一开始不能直接赋值为2,因为可能会是一颗空树
- memset(ch,,sizeof(ch));
- memset(book,,sizeof(book));
- int ans=;
- for(int i=; i<n; i++)
- {
- scanf("%s",a);
- len=strlen(a);
- if(insertt(a)==)
- ans=;
- }
- if(ans==)//不存在前缀关系的情况输出YES,审题
- printf("YES\n");
- else
- printf("NO\n");
- }
- return ;
- }
POJ3630-Phone List-Trie字典树模板题的更多相关文章
- 字典树模板题(统计难题 HDU - 1251)
https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...
- CH 1601 - 前缀统计 - [字典树模板题]
题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...
- HDU 1251 统计难题(字典树模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...
- HDU - 1251 字典树模板题
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部 ...
- (模板)hdoj1251(字典树模板题)
题目链接:https://vjudge.net/problem/HDU-1251 题意:给定一系列字符串之后,再给定一系列前缀,对每个前缀查询以该字符串为前缀的字符串个数. 思路: 今天开始学字典树, ...
- P1184 高手之在一起(字典树模板题,hash算法, map)
哎,唯一值得说明的是,这道题的输入有bug 先把字典树的算法模板放一下 #include<iostream> #include<cstring> using namespace ...
- HDU 2072 - 单词数 - [(有点小坑的)字典树模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件没有 ...
- trie字典树模板浅析
什么是trie? 百度百科 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的 ...
- hdu 1251 字典树模板题 ---多串 查找单词出现次数
这道题题目里没有给定数据范围 我开了2005 疯狂的WA 然后开了50000, A掉 我以为自己模板理解错 然后一天没吃饭,饿得胃疼还是想着把这题A掉再去吃,谁知竟然是这样的问题,,,呵呵~~~ ...
随机推荐
- ubuntu+VS code+launch.json+task.json
1.ubuntu->vs code . 通过官方PPA安装Ubuntu make sudo add-apt-repository ppa:ubuntu-desktop/ubuntu-make s ...
- 递归中,调用forEach方法问题
1 function traverse(objNmae,obj,url){ url = url || objNmae; if(typeof obj === "object" ){ ...
- PAT甲级——A1130 Infix Expression【25】
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- nginx 配置反向代理和静态资源
https://unit.nginx.org/integration/ 与NGINX集成 在NGINX后面安装单元 将NGINX配置为静态Web服务器,并在Unit前面配置反向代理. NGINX直接从 ...
- USACO 2014 US Open Odometer /// 枚举
题目大意: 给定区间 l r 求区间包含多少个数 它们各个位的数只有一个不一样 注意 多个位但多个数为0单个数为x的情况 这种情况只有 x000 即把单个数放在首位才是正确的 同样注意 多个位但单个数 ...
- java全栈商业小程序开发
此次开发只为学习和巩固,第一次学习开发 一.开发前需要了解: 开发框架MVVM.痛点.开源工具.VUE前端框架.微信支付模块.uni-app前端框架.小程序申请.开发工具下载.编写测试小程序.小程序结 ...
- 2018-8-10-win10-uwp-json
title author date CreateTime categories win10 uwp json lindexi 2018-08-10 19:16:53 +0800 2018-2-13 1 ...
- RK3288 android切换耳麦通道
通过耳机状态切换耳机mic与板子麦/work/rk3288/firefly-rk3288_android5.1_git_20180126/kernel/sound/soc/codecs/es8323. ...
- Spark Core 1.3.1源码解析及个人总结
本篇源码基于赵星对Spark 1.3.1解析进行整理.话说,我不认为我这下文源码的排版很好,不能适应的还是看总结吧. 虽然1.3.1有点老了,但对于standalone模式下的Master.Worke ...
- 第七章 yaml格式
一.简单说明 yaml是一个可读性高,用来表达数据序列的格式.YAML 的意思其实是:仍是一种标记语言,但为了强调这种语言以数据做为中心,而不是以标记语言为重点 二.基本语法 缩进时不允许使用Tab键 ...