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 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
随机推荐
- 新手入门学习angular.js的心得体会
看了一天的angular.js,只要记住这是关于双向数据绑定 和单向数据绑定就可以,看看开发文档,短时间内还是可以直接入手的,看个人理解能力(我是小白). 这几天开始着手学习angularjs的有关知 ...
- 猜数字游戏的提示(Master-Mind Hints, UVa 340)
实现一个经典"猜数字"游戏. 给定答案序列和用户猜的序列,统计有多少数字位置正确 (A),有多少数字在两个序列都出现过但位置不对(B). 输入包含多组数据.每组输入第一行为序列长度 ...
- 啥是SQL?
SQL:(Structured Query Language)是结构化查询语言缩写.是一门专门与数据库管理系统打交道的语言. SQL语言:是关系型数据库的标准语言,,其主要用于存取数据,查询数据,更新 ...
- STM32学习笔记:读写内部Flash(介绍+附代码)
一.介绍 首先我们需要了解一个内存映射: stm32的flash地址起始于0x0800 0000,结束地址是0x0800 0000加上芯片实际的flash大小,不同的芯片flash大小不同. RAM起 ...
- 【codeforces 711B】Chris and Magic Square
[题目链接]:http://codeforces.com/contest/711/problem/B [题意] 让你在矩阵中一个空白的地方填上一个正数; 使得这个矩阵两个对角线上的和; 每一行的和,每 ...
- CTF密码学总结
CTF中那些脑洞大开的编码和加密 摘自:https://www.cnblogs.com/mq0036/p/6544055.html 0x00 前言 正文开始之前先闲扯几句吧,玩CTF的小伙伴也许会遇到 ...
- [bzoj4010][HNOI2015]菜肴制作_贪心_拓扑排序
菜肴制作 bzoj-4010 HNOI-2015 题目大意:给定一张n个点m条边的有向图,求一个toposort,使得:(1)满足编号为1的点尽量在前:(2)满足(1)的情况下编号为2的点尽量在前,以 ...
- mapreduce v1.0学习笔记
它是什么? 一个用于处理大数据开源的分布式计算框架,它由java实现,原生提供java编程交互接口,其它语言通过hadoop streaming方式和mapreduce框架交互. 可以做什么? 利用框 ...
- oracle latch
(转载 : http://www.dbtan.com/2010/05/latch-free.html) Latch Free(闩锁释放):Latch Free通常被称为闩锁释放,这个名称常常引起误解, ...
- Oracle Auto Increment Column - Sequence as Default Value
Solution 1: Prior to Oracle 11g, sequence assignment to a number variable could be done through ...