题意

给定 n个长度不超过 10的数字串,问其中是否存在两个数字串S,T ,使得 S是 T的前缀,多组数据。

题目

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

分析

用字典树,第一个为空点**(++tot易错)**连接第一个数(0~9),每出现一个新点,插入字典书中。我的代码是在建树过程中找到出现的前缀,并记录。出现前缀有两种情况:

  • .本身为树上某数字串的前缀,则在建树过程中没有出现新点。
  • .树上的某数字串为当前数字串的前缀,则建树过程中,扫过数字串的末尾(可以用book数组标记数字串结尾)搞了两天,终于弄懂了,相对还是容易的。
  • 复杂度:O(nlogn+nlog2^{2}2n)

AC代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=6e5+10;
const int Z=50;
int T,n,tot,ch[N][Z];
char s[Z];
bool bo[N];
bool insert(char *s)
{int l=strlen(s);int u=1;bool flag=false;for(int i=0; i<l; i++){int c=s[i]-'0';if(!ch[u][c])ch[u][c]=++tot;else if(i==l-1)flag=true;u=ch[u][c];if(bo[u])flag=true;}bo[u]=true;return flag;
}
int main()
{scanf("%d",&T);while(T--){scanf("%d",&n);tot=1;memset(ch,0,sizeof(ch));memset(bo,false,sizeof(bo));bool ans=false;for(int i=1; i<=n; i++){scanf("%s",s);if(insert(s))ans=true;}if(ans)printf("NO\n");else printf("YES\n");}return 0;
}

Phone List POJ - 3630(字典树模板题)相关推荐

  1. 单词数 HDU - 2072(字典树模板题amp;stl)

    lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数.下面你的任务是帮助xiaoou333解决这个问题. Input 有多组数据,每组一行,每组就 ...

  2. Poj 2001 Shortest Prefix(字典树模板)

    字典树入门题...... 只需在对应结点记录num值,找到num为1时的前缀,是唯一前缀(只是该单词的前缀),或者一直找找到该单词结尾. #include <iostream> #incl ...

  3. hdu1305 字典树水题

    题意:      给你一些字符串,然后问你他们中有没有一个串是另一个串的前缀. 思路:       字典树水题,(这种水题如果数据不大(这个题目不知道大不大,题目没说估计不大),hash下也行,把每个 ...

  4. 字典树模板+洛谷P2580 于是他错误的点名开始了

    题目: 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边搓炉石一边点名以至于有一天他连续点到了某个同学两次,然后正好被路过的校长发现了然后就是一顿欧拉欧拉欧拉(详情请见已结束比赛 CON ...

  5. 线段树模板题3:区间染色问题

    1.3线段树模板题3:区间染色问题 在DotA游戏中,帕吉的肉钩实际上是大多数英雄中最恐怖的东西.挂钩由长度相同的几个连续的金属棍组成. 现在,帕吉(Pudge)希望对挂接进行一些操作. 让我们将钩子 ...

  6. J.哭泣的阿木木(线段树模板题)

    哭泣的阿木木 Description 没啥用的背景故事: 在远古的恕瑞玛,有一个孤独而又忧郁的灵魂,阿木木.他在世间游荡,只为找到一个朋友.他遭受了一种远古的巫术诅咒,注定忍受永世的孤单,因为被他触碰 ...

  7. POJ 1451 T9 (字典树好题)

    背景:为了方便九宫格手机用户发短信,希望在用户按键时,根据提供的字典(给出字符串和频数),给出各个阶段最有可能要打的单词. 题意: 首先给出的是字典,每个单词有一个出现频率.然后给出的是询问,每个询问 ...

  8. hdu1156(简单线段树 模板题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  9. hdu 1671 Phone List 字典树模板

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

最新文章

  1. 使用OpenSSL实现证书操作
  2. css表格文字超数量就竖排_绝了,超轻量级中文 OCR,你值得拥有
  3. codeforces contest 1140(D~G)
  4. C#编程中的66个好习惯,你有多少个?(转)
  5. [js] 写一个方法获取图片的方向
  6. 在Ubuntu环境下使用vcpkg安装sqlite_orm包文件
  7. 华为Android10版怎么截屏,安卓手机截图方法 华为手机如何截图 - 云骑士一键重装系统...
  8. c# https请求
  9. 动物笼行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
  10. VB选择文件夹并取文件夹名
  11. 用资源管理器右键编译 Visual Studio 解决方案文件
  12. Tensorflow:操作执行原理
  13. jquery easyui里datagrid用法记录
  14. C++--第17课 - 继承与多态 - 上
  15. OFFICE技术讲座:WP布局方向有哪些
  16. JSON 在线工具 BeJson
  17. 鸿蒙系统的软件怎么下载,鸿蒙系统官网下载软件电脑版
  18. 【杭研大咖说】Istio进入1.7版本,Service Mesh 落地还有什么障碍?
  19. webstorm禁用拼写检查
  20. 【学习笔记】利用API进行数据采集或获取

热门文章

  1. Android之提示This version of Android Studio cannot open this project, please retry with Android Studio
  2. Android studio导入项目提示The same input jar [*.jar] is specified twice
  3. OSI七层模型的作用
  4. node.js npm常用命令
  5. c语言创建新指针,如何用c语言创建一个指针
  6. 下载matlab安装包太慢_MATLAB 2020a商业数学中文版软件下载安装教程
  7. 利用for循环调用插入方法批量插入 一条失败_算法与数据结构(1):基础部分——以插入排序为例...
  8. 早上起床时需要的重力
  9. 求护士的心理阴影面积 | 今日最佳
  10. 动画演示男性结扎手术 | 今日趣图