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树的更多相关文章

  1. hdu 1671&& poj 3630 (trie 树应用)

    Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25280   Accepted: 7678 Descr ...

  2. poj 2945 trie树统计字符串出现次数

    用记录附加信息的val数组记录次数即可. trie的原理:每个可能出现的字目给一个编号c,那么整个树就是一个c叉树 ch[u][c]表示 节点u走c边过去之后的节点 PS:trie树还有种动态写法,使 ...

  3. POJ 2945 trie树

    Find the Clones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7704 Accepted: 2879 Descr ...

  4. POJ 2513 trie树+并查集判断无向图的欧拉路

    生无可恋 查RE查了一个多小时.. 原因是我N define的是250500 应该是500500!!!!!!!!! 身败名裂,已无颜面对众人.. 吐槽完了 我们来说思路... 思路: 判有向图能否形成 ...

  5. Phone List POJ 3630 Trie Tree 字典树

    Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29416   Accepted: 8774 Descr ...

  6. POJ 3630 Phone List(trie树的简单应用)

    题目链接:http://poj.org/problem?id=3630 题意:给你多个字符串,如果其中任意两个字符串满足一个是另一个的前缀,那么输出NO,否则输出YES 思路:简单的trie树应用,插 ...

  7. poj 3630 Phone List trie树

    Phone List Description Given a list of phone numbers, determine if it is consistent in the sense tha ...

  8. POJ 3630 Phone List | Trie 树

    题目: 给定 n 个长度不超过 10 的数字串,问其中是否存在两个数字串 S, T ,使得 S 是 T 的前缀.多组数据,数据组数不超过 40. 题解: 前缀问题一般都用Trie树解决: 所以跑一个T ...

  9. poj 2513 Colored Sticks (trie 树)

    链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...

随机推荐

  1. 解决hibernate删除时的异常 deleted object would be re-saved by cascade (remove deleted object from associa

    今天在做项目时,需要删除一个对象,由于关联关系是一对多和多对一的关系,于是在代码中需要删除多的一方的对象时出现了 deleted object would be re-saved by cascade ...

  2. JAVA如何获得数据库的字段及字段类型

    Java获取数据库的表中各字段的字段名,代码如下: import java.sql.Connection;import java.sql.DriverManager;import java.sql.R ...

  3. HOG特征过程解释(转)

    1.HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子.它通过计算和统计图像局部区域的 ...

  4. 手机访问pc版网站自动跳转为手机版页面

    1.PC版首页</head>标签前加上以下脚本 <script src="/tools/browser_redirect.ashx"></script ...

  5. 【 Codeforces Global Round 1 B】Tape

    [链接] 我是链接,点我呀:) [题意] x轴上有m个连续的点,从1标号到m. 其中有n个点是特殊点. 让你用k段区间将这n个点覆盖. 要求区间的总长度最小. [题解] 一开始假设我们需要n个胶带(即 ...

  6. [APIO2014] [Uoj103] [Bzoj3676] Palindromes回文串 [Manacher,后缀数组]

    用Manacher算法枚举回文子串,每次在后缀数组排序后的后缀数组中二分,因为用某一后缀和其他子串分别求匹配的长度,匹配长度在排序后该后缀的两侧具有单调性(匹配长度为min{H[x]|i<=x& ...

  7. CODEVS3147 矩阵乘法2

    ...怎么优化都是90分,最后一个点一直T掉,有谁过了请告诉我. Program CODEVS3147; ; ..maxn,-..maxn] of longint; n,q,i,j,k,k1,k2,k ...

  8. PatentTips - Invalidating TLB entries in a virtual machine system

    BACKGROUND This invention relates to virtual machines. In particular, the invention relates to trans ...

  9. Servlet的HttpServletResponse输出

    了解其中的一些字符设置,PrintWriter输出等.. form.html: <!DOCTYPE html> <html> <head> <title> ...

  10. hdu_1020_Encoding_201310172120

    Encoding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...