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

  1. 2 3 911 97625999 91125426 5 113 12340 123440 12345 98346

Sample Output

  1. NO YES
  1. 题意:判断每组串是不是某个串的前缀。
  1. sl:字典树轻松解决,沿途判断是不是经过单词节点,如果当前单词判断完了那么判断当前节点是不是有后继节点
  1. ps:差点爆内存,最好用左儿子右兄弟。
  1.  1 #include<cstdio>
  2.  2 #include<cstring>
  3.  3 #include<algorithm>
  4.  4 using namespace std;
  5.  5 const int MAX = ;
  6.  6 const int A =  ;
  7.  7 char num[MAX];
  8.  8 int flag;
  9.  9 int ch[A][];
  10.  struct Trie
  11.  {
  12.      int val[A]; int sz;
  13.      Trie(){sz=; memset(ch[],,sizeof(ch[])); }
  14.      int index(char a) {return a-'';}
  15.      void insert(char *s,int v)
  16.      {
  17.          int u=,c; int n=strlen(s);
  18.          for(int i=;i<n;i++)
  19.          {
  20.              c=index(s[i]);
  21.              if(!ch[u][c])
  22.              {
  23.                  memset(ch[sz],,sizeof(ch[sz]));
  24.                  val[sz]=;
  25.                  ch[u][c]=sz++;
  26.              }
  27.              u=ch[u][c];
  28.              if(val[u]!=) flag=;
  29.          }
  30.          val[u]=v;
  31.          for(int i=;i<;i++) if(ch[u][i]) flag=;
  32.      }
  33.  };
  34.  int main()
  35.  {
  36.      int cas,n;
  37.      scanf("%d",&cas);
  38.      while(cas--)
  39.      {
  40.          Trie trie;
  41.          scanf("%d",&n); flag=;
  42.          for(int i=;i<n;i++)
  43.          {
  44.              scanf("%s",num);
  45.              trie.insert(num,i+);
  46.          }
  47.          if(flag) printf("NO\n");
  48.          else printf("YES\n");
  49.      }
  50.  }
  51.  /*
  52.  5 5
  53.  123456
  54.  6656
  55.  3123
  56.  13646
  57.  1564
  58.  */

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. jQuery多项选择器

    jQuery多项选择器模式: $("selector1,selector2,selectorN"); 将每一个选择器匹配到的元素合并后一起返回,可以指定任意多个选择器,并将匹配到的 ...

  2. 学生党的Surface Pro 5乞丐版使用体验

    因为已经装了台式机,大一开学时买的厚重且续航差的华硕游戏本(i5+GTX950M+8G)对我这个考研党已经显得不合适了.恰巧有一同学笔记本坏了,我便将游戏本低价出了,然后用两三倍的价格,入手了surf ...

  3. 思维/构造 HDOJ 5353 Average

    题目传送门 /* 思维/构造:赛后补的,当时觉得3题可以交差了,没想到这题也是可以做的.一看到这题就想到了UVA_11300(求最小交换数) 这题是简化版,只要判断行不行和行的方案就可以了,做法是枚举 ...

  4. Oracle 参考脚本

    一.创建物化视图 --新建表空间 CREATE TABLESPACE MLOG_TBS LOGGING DATAFILE 'mlog_tbs.dbf' SIZE 32M AUTOEXTEND ON N ...

  5. 选择语言之switch case

    程序语言-选择语言之switch   case 多选一,类似if    else if  else if  else 模版: Switch(选择条件) { Case(条件一)//相当于if Conso ...

  6. myBatis逆向生成及使用

    引入数据库驱动 <!-- mybatis逆向生成包 --><dependency> <groupId>org.mybatis.generator</group ...

  7. Spring:(三) --常见数据源及声明式事务配置

    Spring自带了一组数据访问框架,集成了多种数据访问技术.无论我们是直接通过 JDBC 还是像Hibernate或Mybatis那样的框架实现数据持久化,Spring都可以为我们消除持久化代码中那些 ...

  8. 联想 Z5S(L78071)免解锁BL 免rec 保留数据 ROOT Magisk Xposed 救砖 ZUI 10.5.370

    >>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...

  9. ElasticSearch 安装使用

    安装: 1.下载ElasticSearch.解压到相关文件夹 2.运行elasticsearch.bat,启动程序 3.在浏览器输入:http://localhost:9200/,显示相关Es内容即安 ...

  10. JS高级——Blob处理二进制文件

    https://www.cnblogs.com/hhhyaaon/p/5928152.html