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. bzoj 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果【tarjan+记忆化搜索】

    对这个奇形怪状的图tarjan,然后重新连边把图变成DAG,然后记忆化搜索即可 #include<iostream> #include<cstdio> using namesp ...

  2. ----堆栈 STL 函数库 ----有待补充

    #include<cstdio> #include<string> #include<vector> #include<iostream> using ...

  3. Y-C

    1.asp.net服务控件生命周期 11个生命阶段 (1)初始化: 初始化在传入Web请求生命周期内所需的设置,.跟踪视图状态.页面框架通过默认方式引发Init事件,并调用OnInit()方法,控件开 ...

  4. yii框架下jquery在ajax更新后失效问题

    解决方案,以live的形式重新绑定一次, /***回复隐藏收起效果***/ $(".btn-reply").live('click',function(event){ var da ...

  5. 理解 Java 构造函数不可以继承

    参考来源:http://www.52bowen.com/a/2604620.html

  6. CSS之浮动元素

    <html> <head> <meta charset="utf-8"> <title></title> <sty ...

  7. linux下jdk与tomcat的安装与配置

    Linux中jdk与tomcat的安装与配置 1.搭建环境: (1)Linux环境:CentOS6.1 (2)jdk-1.8 (3)tomcat-9.0 2.在Linux系统上创建目录 在usr/lo ...

  8. Angular——单页面与路由的使用

    单页面 SPA(Single Page Application)指的是通单一页面展示所有功能,通过Ajax动态获取数据然后进行实时渲染,结合CSS3动画模仿原生App交互,然后再进行打包(使用工具把W ...

  9. 梦想CAD控件 2019.01.20更新

    下载地址:http://www.mxdraw.com/ndetail_10120.html1. 修改CAD不等比例块保存问题2. 修改CAD捕捉时,Z值对捕捉不准的影响3. 修改图片对象选择后,自动跑 ...

  10. 01Microsoft SQL Server

    Microsoft SQL Server Microsoft SQL Server 是Microsoft 公司推出的关系型数据库管理系统.具有使用方便可伸缩性好与相关软件集成程度高等优点,可跨越膝上型 ...