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 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
随机推荐
- iDRAC RAC0218 最大会话限制
用ssh工具登陆IDRAC远程管理ip地址: /admin1-> racadm racreset RAC reset operation initated successfully. It m ...
- 洛谷——P1759 通天之潜水
P1759 通天之潜水 题目背景 直达通天路·小A历险记第三篇 题目描述 在猴王的帮助下,小A终于走出了这篇荒山,却发现一条波涛汹涌的河拦在了自己的面前.河面上并没有船,但好在小A有n个潜水工具. ...
- 198. House Robber(动态规划)
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Android学习总结(5)——9个非常有用的Andorid 程序片段
一,获取系统版本号: PackageInfo info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0); int ...
- JavaSE 学习笔记之多态(七)
多 态:函数本身就具备多态性,某一种事物有不同的具体的体现. 体现:父类引用或者接口的引用指向了自己的子类对象.//Animal a = new Cat(); 多态的好处:提高了程序的扩展性. 多态的 ...
- Android RecyclerViewSwipeDismiss:水平、垂直方向的拖曳删除item
Android RecyclerViewSwipeDismiss:水平.垂直方向的拖曳删除item RecyclerViewSwipeDismiss是一种支持RecyclerView的水平.垂直 ...
- 高三感想+跨年感想+OI回忆录
2017.12.29晚 高三感慨 与OI分别很久了,发现自己已经不会敲代码了…… 高三国庆前大概比较正常,只是某只人影响有点大(现在好多了)…… 感觉国庆以后状态就没好过. 先感冒两个星期,然后某个周 ...
- [cogs396] [网络流24题#4] 魔术球 [网络流,最大流,最小路径覆盖]
本题枚举每多一个球需要多少个柱子,可以边加边边计算,每次只需要判断$i-Dinic()$即可:特别注意边界. #include <iostream> #include <algori ...
- N天学习一个Linux命令之free
用途 查看系统内存(物理/虚拟/缓存/共享)使用情况 用法 free [-b | -k | -m | -g | -h] [-o] [-s delay ] [-c count ] [-a] [-t] [ ...
- linux环境下安装varnish
Varnish是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好. sudo apt-get insta ...