Phone List
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 34805   Accepted: 9980

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

Source

题意:

给n个最多是10位的电话号码,问存不存在某个电话号码是其他电话号码的前缀的情况。存在输出NO,否则输出YES

思路:

最开始的思路是先给所有的字符按照字符串的长度从短到长排个序,一次将字符串添加到Trie树中。添加过程中如果发现某个节点已经被标记为字符串结束,那么说明要输出NO。这样的思路TLE。大概是因为对于每一个字符串都要先求一下长度然后排序的话就变成了O(n^2logn)?

其实我们并不需要排序。只需要在建立Trie树的时候对节点的出现次数进行统计。并且,一条路径上靠近根节点的节点的次数一定是大于等于远离根节点的。建立完成后我们对于每一个字符串,去找他在树上的这个路径是不是全部都是次数大于1的节点。只要找到一个路径全部都是大于1的节点的路径说明这个字符串是某个字符串的前缀。输出NO

 #include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f int t, n;
const int maxn = 1e5 + ;
int ed[maxn];
int trie[maxn][], tot = ;
//vector<string>s;
//string s[maxn];
char s[maxn][]; void insertt(char* str)
{
int len = strlen(str), p = ;
for(int k = ; k < len; k++){
int ch = str[k] - '';
if(trie[p][ch] == ){
trie[p][ch] = tot++;
}
p = trie[p][ch];
ed[p]++;
}
//ed[p] = true;
//ed[p] = true;
} bool searchh(char *str)
{
int len = strlen(str), p = ;
for(int k = ; k < len; k++){
p = trie[p][str[k] - ''];
if(ed[p] == ){
return false;
}
}
return true;
} bool cmp(string a, string b)
{
return a.size() > b.size();
//return strlen(a) > strlen(b);
} int main()
{
scanf("%d", &t);
while(t--){
tot = ;
memset(trie, , sizeof(trie));
memset(ed, , sizeof(ed));
scanf("%d", &n);
for(int i = ; i < n; i++){
/*char c[10];
string ch;
scanf("%s", c);
ch = c;
s.push_back(ch);
//cin>>s[i];*/
scanf("%s", &s[i]);
insertt(s[i]);
}
//sort(s.begin(), s.end(), cmp);
/*for(int i = 0; i < n; i++){
cout<<s[i]<<endl;
}*/
bool flag = true;
for(int i = ; i < n; i++){
if(searchh(s[i])){
flag = false;
break;
}
/*if(searchh((char*)s[i].data())){
printf("NO\n");
flag = false;
break;
}
else{
insertt((char*)s[i].data());
}*/
}
if(flag){
printf("YES\n");
}
else{
printf("NO\n");
}
}
return ;
}

poj3630 Phone List【Trie树】的更多相关文章

  1. poj3630 Phone List (trie树模板题)

    Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26328   Accepted: 7938 Descr ...

  2. HihoCoder第二周与POJ3630:Trie树的建立

    这又是两道一样的题,都是建立trie树的过程. HihoCoder第二周: 这里其实逻辑都很简单,主要在于数据结构struct的使用. #include <iostream> #inclu ...

  3. POJ3630——简单Trie树

    这个题的意思是说,给出一些字符串,判断是否有字符串是另一个字符串的前缀,当然可以用排序水过,不过这个题拿来练习一下Trie树不错. 这个题在poj的discuss上好多人说必须要静态建树,估计都是用了 ...

  4. 蒟蒻的trie树专题

    POJ 3630 Phone List: 模板 ///meek #include<bits/stdc++.h> using namespace std; using namespace s ...

  5. 基于trie树做一个ac自动机

    基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...

  6. 基于trie树的具有联想功能的文本编辑器

    之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...

  7. hihocoder-1014 Trie树

    hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...

  8. 洛谷P2412 查单词 [trie树 RMQ]

    题目背景 滚粗了的HansBug在收拾旧英语书,然而他发现了什么奇妙的东西. 题目描述 udp2.T3如果遇到相同的字符串,输出后面的 蒟蒻HansBug在一本英语书里面找到了一个单词表,包含N个单词 ...

  9. 通过trie树实现单词自动补全

    /** * 实现单词补全功能 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #incl ...

随机推荐

  1. Oracle数据库表空间与数据文件的关系描述正确的是( )

    Oracle数据库表空间与数据文件的关系描述正确的是( ) A.一个表空间只能对应一个数据文件 B.一个表空间可以对应多个数据文件 C.一个数据文件可以对应多个表空间 D.表空间与数据文件没任何对应关 ...

  2. (转)MFC:Windows如何区分鼠标双击和两次单击

    在Windows平台上,鼠标左键的按下.松开.快速的两次点击会产生WM_LBUTTONDOWN.WM_LBUTTONUP和WM_LBUTTONDBLCLK消息,但是Windows根据什么来区分连续的两 ...

  3. asp.net mvc中用angularJs写的增删改查的demo。初学者,求指点。。

    直接给个代码下载链接.... http://pan.baidu.com/s/1FfVgq 本人刚刚学习angularJs,感觉双向数据绑定蛮爽的... 之前的代码存在点问题,已修复

  4. php如何解决多线程同时读写一个文件的问题

    <?php header("content-type:text/html;charset=utf-8"); $fp = fopen("lock.txt", ...

  5. 一、JSON解析与字符串化

    JSON.stringify() 序列化对象.数组或原始值 语法:JSON.stringify(o,filter,indent) o,要转换成JSON的对象.数组或原始值 filter,指定要序列化的 ...

  6. gradle教程 [原创](eclipse/ADT下 非插件 非Android Studio/AS)纯手打 第三篇:gradle完整的实战

    上两篇的地址 安装配置 http://www.cnblogs.com/uncle2000/p/4276833.html 简单实战 http://www.cnblogs.com/uncle2000/p/ ...

  7. MathType编辑物理单位的方法

    在用MathType编辑物理公式时,由于物理单位很多都是复合单位,所以在编辑时如果能够有这种复合单位直接使用的话,编辑效率就会大大提高.实际上这种想法在MathType中是可行的,MathType中也 ...

  8. SQL中查询语句的使用

    常用SQL查询语句 - myLittleGarden - 博客园 http://www.cnblogs.com/sunada2005/p/3411873.html 一.简单查询语句 1. 查看表结构 ...

  9. mysql数据库中,通过mysqldump工具仅将某个库的所有表的定义进行转储

    需求描述: 在研究mysqldump工具的使用,想的是如何将某个库下的,或者某个表的表的定义(表结构创建语句)进行转储 操作过程: 1.通过--no-data参数,就可以将某个库的表定义进行转储 [m ...

  10. chrome JS 总结

    1. chrome 的 console 中不能添加本地文件 2. 下面的代码是在亚马逊的商品页面上添加一个 image, 点击之后触发 alert 函数. 其中 cBoxInner 是人工寻找到的标签 ...