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. recast 生成navmesh主要流程

    参考:      critterai  http://www.critterai.org      recast & Detour https://github.com/recastnavig ...

  2. robotframework - 测试用例&套件- Settings标签

    1.Test Case -- Settings标签截图 2.Test Case Settings 标签说明: Documentation:用于描述用例的一个小文本,它可以把 URL 地址转换为可点击的 ...

  3. [Usaco2018 Open]Disruption

    Description Farmer John自豪于他所经营的交通发达的的农场.这个农场是由N块牧场(2≤N≤50,000)组成的,N-1条双向道路将它们连接起来,每一条道路的都为一单位长度.Farm ...

  4. Linux学习之路2 Bash的基本操作

    一.SHELL的介绍 shell分为两种:CLI(command Line Interface)和GUI(Graphical User Interface) 操作系统中的shell: GUI:GNOM ...

  5. LPS HDOJ 4745 Two Rabbits

    题目传送门 /* 题意:一只兔子顺时针跳,另一只逆时针跳,跳石头权值相等而且不能越过起点 LPS:这道就是LPS的应用,把环倍增成链,套一下LPS,然而并不能理解dp[i][i+n-2] + 1,看别 ...

  6. 全面学习ORACLE Scheduler特性(4)创建和管理Schedule

    三.使用Schedules 10g 中新推出的SCHEDULER可能确实会让很多初接触的朋友感觉晕头晕脑,相比之前的jobs,SCHEDULER中新增的概念太多.比如说jobs,仍然可以理解成之前版本 ...

  7. 238 Product of Array Except Self 除自身以外数组的乘积

    一个长度为 n 的整形数组nums,其中 n > 1,返回一个数组 output ,其中 output[i] 等于nums中除nums[i]以外所有元素的乘积.不用除法 且在O(n)内解决这个问 ...

  8. Spring Boot (32) Lock 本地锁

    平时开发中,有时会双击提交表单造成重复提交,或者网速比较慢时还没有响应又点击了按钮,我们在开发中必须防止重复提交 一般在前台进行处理,定义个变量,发送请求前判断变量值为true,然后把变量设置为fal ...

  9. iOS popViewControllerAnimated后刷新原先的表格

    当主页面列表push子页面,子页面修改后pop回主页面后应该刷新主页面列表数据,不修改子页面信息就不刷新主页面列表,这里介绍个取巧的方法:利用[NSNotificationCenter default ...

  10. vue2.0之60s验证码发送

    快速的说下我的60s经历不管移动还是pc端的登录都需要发送验证信息,那么我们熟悉的那个验证按钮就不可少了.首先,我们都知道的一些基本功能.1.验证账号输入的格式正确与否(减少传递基本的错误信息)2.@ ...