POJ 3630 trie树
Phone List
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 26559 Accepted: 8000
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
题意:t个case(1<=t<=40),给你n个电话号码(电话号码长度<10)(1 ≤ n ≤ 10000),如果有电话号码是另一个电话号码的前缀,则称这个通讯录是不相容的,判断通讯录是否相容。
思路:
trie树(静态),动态分配内存会超时(最多4000000个new……)
把电话的结尾做标记,check的过程中,如果是电话号码的结尾并且trie树后面还有枝子,输出NO。(对树DFS一下)
每次新建枝子的时候要记得把枝子后面的设成初值(-1),这样就不用每个case对树DFS清零了。我开了一个l数组代表电话的长度(其实有点儿多余。)
代码实现得不好看,将就看吧。。。
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct trie
{
bool x;
int next[10];
}y[100005];
char a[10001][10],flag;
int CASE,n,l[10001],tot;
void insert(int q)
{
int p=0;
for(int i=0;i<l[q];i++)
{
if(y[p].next[a[q][i]]==-1)
{
tot++;
y[p].next[a[q][i]]=tot;
for(int i=0;i<10;i++)
y[tot].next[i]=-1;
y[tot].x=0;
p=tot;
}
else
p=y[p].next[a[q][i]];
}
y[p].x=1;
}
void check(int x)
{
for(int i=0;i<10;i++)
{
if(~y[x].next[i])
{
if(y[x].x!=y[y[x].next[i]].x)
{
for(int ii=0;ii<10;ii++)
if(~y[y[x].next[i]].next[ii]) flag=1;
check(y[x].next[i]);
}
else check(y[x].next[i]);
}
}
}
int main()
{
scanf("%d",&CASE);
while(CASE--)
{
tot=0;
for(int i=0;i<10;i++)
y[0].next[i]=-1;
flag=y[0].x=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",a[i]);
l[i]=strlen(a[i]);
for(int j=0;j<l[i];j++)
a[i][j]-='0';
insert(i);
}
check(0);
if(flag)printf("NO\n");
else printf("YES\n");
}
}
一次AC~
这位前辈是怎么用0msAC的!!! 代码量又这么少。。佩服啊
POJ 3630 trie树的更多相关文章
- hdu 1671&& poj 3630 (trie 树应用)
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25280 Accepted: 7678 Descr ...
- poj 2945 trie树统计字符串出现次数
用记录附加信息的val数组记录次数即可. trie的原理:每个可能出现的字目给一个编号c,那么整个树就是一个c叉树 ch[u][c]表示 节点u走c边过去之后的节点 PS:trie树还有种动态写法,使 ...
- POJ 2945 trie树
Find the Clones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7704 Accepted: 2879 Descr ...
- POJ 2513 trie树+并查集判断无向图的欧拉路
生无可恋 查RE查了一个多小时.. 原因是我N define的是250500 应该是500500!!!!!!!!! 身败名裂,已无颜面对众人.. 吐槽完了 我们来说思路... 思路: 判有向图能否形成 ...
- Phone List POJ 3630 Trie Tree 字典树
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29416 Accepted: 8774 Descr ...
- POJ 3630 Phone List(trie树的简单应用)
题目链接:http://poj.org/problem?id=3630 题意:给你多个字符串,如果其中任意两个字符串满足一个是另一个的前缀,那么输出NO,否则输出YES 思路:简单的trie树应用,插 ...
- poj 3630 Phone List trie树
Phone List Description Given a list of phone numbers, determine if it is consistent in the sense tha ...
- POJ 3630 Phone List | Trie 树
题目: 给定 n 个长度不超过 10 的数字串,问其中是否存在两个数字串 S, T ,使得 S 是 T 的前缀.多组数据,数据组数不超过 40. 题解: 前缀问题一般都用Trie树解决: 所以跑一个T ...
- poj 2513 Colored Sticks (trie 树)
链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
随机推荐
- scss基础
1.变量$ 全局 局部 .div{ $color:yellow; } 2.类似函数@mixin border-radius($radius) { }引用:@include border-radius( ...
- LINUX-查看进程内环境变量
ps -ef find PID cat /proc/$PID/environ | grep ENV
- sysbench测试阿里云CPU
参考 https://wiki.mikejung.biz/Benchmarking 买了一个1核的ECS,测试一下CPU性能 第一次是只用1个thread去跑 [root@iZwz9fy718twfi ...
- / Vijos / 题库 /1250 / 最勇敢的机器人
/ Vijos / 题库 /1250 / 最勇敢的机器人 借鉴博客:http://www.cnblogs.com/chty/p/5830516.html 背景 Wind设计了很多机器人.但是它们都认为 ...
- Linux判断
#字符串比较if [ "$1" == "判断条件" ] then echo "$1" elif [ "$1" == &q ...
- 【ACM】hdu_1089_A+BI_201307261121
A+B for Input-Output Practice (I)Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Spring MVC-控制器(Controller)-参数方法名称解析器(Parameter Method Name Resolver )示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_parametermethodnameresolver.htm 说明:示例基于Sp ...
- N天学习一个linux命令之diff
用途 按行比较文件差异,也可以比较目录 用法 diff [OPTION]... FILES 常用选项 -i --ignore-case 忽略大小写 --ignore-file-name-case 忽略 ...
- 找出二叉查找树中指定结点的”下一个"结点(也即中序后继)
设计一个算法.找出二叉查找树中指定结点的"下一个"结点(也即中序后继).能够假定每一个结点都含有指向父结点的连接. watermark/2/text/aHR0cDovL2Jsb2c ...
- 基数排序之多keyword排序运用队列
源码例如以下: #include <stdlib.h> #include <stdio.h> typedef struct QUEUEnode* link; struct QU ...