POJ 3630
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 20894 | Accepted: 6532 |
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:
- 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
题意:判断每组串是不是某个串的前缀。
sl:字典树轻松解决,沿途判断是不是经过单词节点,如果当前单词判断完了那么判断当前节点是不是有后继节点
ps:差点爆内存,最好用左儿子右兄弟。
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 using namespace std;
5 const int MAX = ;
6 const int A = ;
7 char num[MAX];
8 int flag;
9 int ch[A][];
struct Trie
{
int val[A]; int sz;
Trie(){sz=; memset(ch[],,sizeof(ch[])); }
int index(char a) {return a-'';}
void insert(char *s,int v)
{
int u=,c; int n=strlen(s);
for(int i=;i<n;i++)
{
c=index(s[i]);
if(!ch[u][c])
{
memset(ch[sz],,sizeof(ch[sz]));
val[sz]=;
ch[u][c]=sz++;
}
u=ch[u][c];
if(val[u]!=) flag=;
}
val[u]=v;
for(int i=;i<;i++) if(ch[u][i]) flag=;
}
};
int main()
{
int cas,n;
scanf("%d",&cas);
while(cas--)
{
Trie trie;
scanf("%d",&n); flag=;
for(int i=;i<n;i++)
{
scanf("%s",num);
trie.insert(num,i+);
}
if(flag) printf("NO\n");
else printf("YES\n");
}
}
/*
5 5
123456
6656
3123
13646
1564
*/
POJ 3630的更多相关文章
- POJ 3630 Phone List(trie树的简单应用)
题目链接:http://poj.org/problem?id=3630 题意:给你多个字符串,如果其中任意两个字符串满足一个是另一个的前缀,那么输出NO,否则输出YES 思路:简单的trie树应用,插 ...
- poj 3630 Phone List(字典树)
题目链接: http://poj.org/problem?id=3630 思路分析: 求在字符串中是否存在某个字符串为另一字符串的前缀: 即对于某个字符串而言,其是否为某个字符串的前缀,或存在某个其先 ...
- 【POJ 3630】 Phone List
[题目链接] http://poj.org/problem?id=3630 [算法] 字典树 [代码] #include <algorithm> #include <bitset&g ...
- POJ 3630 , HDU 1671 Phone List - from lanshui_Yang
这道题也是一道找前缀的问题,很自然地要用到Trie树,但是如果用动态Trie树(即用指针开辟内存)的话,虽然在HDU上可以过(可能是HDU的数据比较水),但在POJ上会TLE , 所以这道题只能用静态 ...
- poj 3630 Phone List
#include<iostream> #include<cstdio> #include<cstring> #define N 100005 using names ...
- HDU 1671 Phone List(POJ 3630)
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- POJ 3630 Phone List Trie题解
Trie的应用题目. 本题有两个难点了: 1 动态建立Trie会超时,须要静态建立数组,然后构造树 2 推断的时候注意两种情况: 1) Tire树有133,然后插入13333556的时候.2)插入顺序 ...
- POJ 3630 Phone List(字符串前缀重复)题解
Description Given a list of phone numbers, determine if it is consistent in the sense that no number ...
- poj 3630 Phone List 贪心
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23722 Accepted: 7289 Descr ...
- poj 3630 Phone List trie树
Phone List Description Given a list of phone numbers, determine if it is consistent in the sense tha ...
随机推荐
- bzoj 4756: [Usaco2017 Jan]Promotion Counting【dfs+树状数组】
思路还是挺好玩的 首先简单粗暴的想法是dfs然后用离散化权值树状数组维护,但是这样有个问题就是这个全局的权值树状数组里并不一定都是当前点子树里的 第一反应是改树状数组,但是显然不太现实,但是可以这样想 ...
- vbnet 进程监控,监控Acad开启的数量,并且添加到开机启动
1# 自定义函数,添加到注册表 Public Sub StartRunRegHKLM() REM HKEY_LOCAL_MACHINE \ SOFTWARE \ WOW6432Node \ Micro ...
- mvn 配置
<!-- 阿里云仓库1 --> <mirror> <id>alimaven-1</id> <name>al ...
- laravel生命周期和核心思想
工欲善其事,必先利其器.在开发Xblog的过程中,稍微领悟了一点Laravel的思想.确实如此,这篇文章读完你可能并不能从无到有写出一个博客,但知道Laravel的核心概念之后,当你再次写起Larav ...
- loadrunner11 安装破解,汉化包
说说自己的心痛史,好不容易安装了loadrunner 11 居然浏览器不支持,我的系统是win8.1,ie浏览器最低支持ie11,我还能说啥子...其他浏览器试过了依旧是不可以!!所以我安装了一个虚拟 ...
- Spring boot 分环境部署
一.如果配置文件为:application.properties时 1.application.properties用于填些公共文件 以下为不同环境的配置文件需要单独配置 application-de ...
- JavaScript--编程练习2
制作一个跳转提示页面: 要求: 1. 如果打开该页面后,如果不做任何操作则5秒后自动跳转到一个新的地址,如慕课网主页. 2. 如果点击“返回”按钮则返回前一个页面. 效果: 注意: 在窗口中运行该程序 ...
- 连接oracle出现的问题以及解决办法
连接oracle出现过的问题: 1,ORA-12514::监听程序当前无法识别链接描述符中请求的服务 1)重启服务,看是否解决 2)测试网络监听是否能监听成功,监听不成功的话,查看下面几个点:服务名( ...
- RabbitMQ指南之四:路由(Routing)和直连交换机(Direct Exchange)
在上一章中,我们构建了一个简单的日志系统,我们可以把消息广播给很多的消费者.在本章中我们将增加一个特性:我们可以订阅这些信息中的一些信息.例如,我们希望只将error级别的错误存储到硬盘中,同时可以将 ...
- [ TJOI 2012 ] 防御
\(\\\) Description 有 \(n\) 人,第 \(i\) 个人有一个护甲值 \(a_i\). 有 \(m\) 次操作,分为以下两种: \(A\ l\ r\ x\) 对编号在 \([l, ...