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

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

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

  2. poj 3630 Phone List(字典树)

    题目链接: http://poj.org/problem?id=3630 思路分析: 求在字符串中是否存在某个字符串为另一字符串的前缀: 即对于某个字符串而言,其是否为某个字符串的前缀,或存在某个其先 ...

  3. 【POJ 3630】 Phone List

    [题目链接] http://poj.org/problem?id=3630 [算法] 字典树 [代码] #include <algorithm> #include <bitset&g ...

  4. POJ 3630 , HDU 1671 Phone List - from lanshui_Yang

    这道题也是一道找前缀的问题,很自然地要用到Trie树,但是如果用动态Trie树(即用指针开辟内存)的话,虽然在HDU上可以过(可能是HDU的数据比较水),但在POJ上会TLE , 所以这道题只能用静态 ...

  5. poj 3630 Phone List

    #include<iostream> #include<cstdio> #include<cstring> #define N 100005 using names ...

  6. HDU 1671 Phone List(POJ 3630)

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. POJ 3630 Phone List Trie题解

    Trie的应用题目. 本题有两个难点了: 1 动态建立Trie会超时,须要静态建立数组,然后构造树 2 推断的时候注意两种情况: 1) Tire树有133,然后插入13333556的时候.2)插入顺序 ...

  8. POJ 3630 Phone List(字符串前缀重复)题解

    Description Given a list of phone numbers, determine if it is consistent in the sense that no number ...

  9. poj 3630 Phone List 贪心

    Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23722   Accepted: 7289 Descr ...

  10. poj 3630 Phone List trie树

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

随机推荐

  1. poj2096Collecting Bugs(概率期望dp)

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 6400   Accepted: 3128 ...

  2. 洛谷P3400 仓鼠窝(单调栈)

    P3400 仓鼠窝 题目描述 萌萌哒的Created equal是一只小仓鼠,小仓鼠自然有仓鼠窝啦. 仓鼠窝是一个由n*m个格子组成的行数为n.列数为m的矩阵.小仓鼠现在想要知道,这个矩阵中有多少个子 ...

  3. CentOS 6.5克隆后eth1与eth0的问题

    CentOS 6.5克隆后eth1与eth0的问题   按照安装文档执行以下步骤时:   从克隆出来的虚拟机网卡都会被命名为eth1,而有些程序或者脚本,涉及到网卡的,默认写的是eth0,这时就存在要 ...

  4. 小记 react 数据存储位置

    react 中状态的六个存储位置 state 我想大家都知道这个地方,而且在使用 setState 时会触发组件的更新 class prop 将值存在 class 的对象中,如: class App ...

  5. 一些常见的iOS面试问题,一眼就能看出 初级和高级工程师的区别

    前言 面试题中有一些一般性的问题,通常是会问到的.面试iOS应聘者时,切入点很重要,不同的切入点会导致不同的结果,没有找到合适的切入点也无法对应聘者有一个全面的了解. 所以下面的面试问题更多的是提供方 ...

  6. PostgreSQL逻辑复制之pglogical篇

    PostgreSQL逻辑复制之slony篇 一.pglogical介绍 pglogical 是 PostgreSQL 的拓展模块, 为 PostgreSQL 数据库提供了逻辑流复制发布和订阅的功能. ...

  7. Uva 10766 Organising the Organisation (Matrix_tree 生成树计数)

    题目描述: 一个由n个部门组成的公司现在需要分层,但是由于员工间的一些小小矛盾,使得他们并不愿意做上下级,问在满足他们要求以后有多少种分层的方案数? 解题思路: 生成树计数模板题,建立Kirchhof ...

  8. 内存溢出及Jvm监控工具

    内存泄露与内存溢出 内存溢出 out of memory,是指程序在申请内存时,没有足够的内存空间供其使用,出现out of memory. 内存泄露 memory leak,是指程序在申请内存后,无 ...

  9. Pycharm消除波浪线

    PyCharm使用了较为严格的PEP8检查规则,稍微有点错误就会出现波浪线提示.那么怎么消除这些波浪线呢?一个简单粗暴的方法就是:在编辑器的右下角有个小人形状的按钮,点开之后有个滚动条,将滚动条滑动到 ...

  10. Greenplum开发

    Greenplum(GP)采用了MPP架构,基于开源的数据库 PostgreSQL(PG). 1.首先什么是MPP架构? GreenPlum的架构采用了MPP(大规模并行处理).在 MPP 系统中,每 ...