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. 【MySQL】MySQL 常用语法之锁表与解锁表

    mysql 锁表语句: Lock锁整张表: 写锁定: LOCK TABLES products WRITE: 写锁,锁定之后,只有当前线程可以进行读操作和写操作,其他线程读操作和写操作均被堵塞.... ...

  2. 修改MySQL的默认密码的四种小方法

    投稿:lijiao 字体:[增加 减小] 类型:转载 时间:2015-09-05我要评论 对于windows平台来说安装完MySQL后,系统就已经默认生成了许可表和账户,下文中就教给大家如何修改MyS ...

  3. 【Java面试题】42 TreeSet里面放对象,如果同时放入了父类和子类的实例对象,那比较时使用的是父类的compareTo方法,还是使用的子类的compareTo方法,还是抛异常!

    应该是没有针对问题的确切的答案,当前的add方法放入的是哪个对象,就调用哪个对象的compareTo方法,至于这个compareTo方法怎么做,就看当前这个对象的类中是如何编写这个方法的 试验如下: ...

  4. uniqid,md5,microtime

    <?php header("content-type:text/html;charset=utf-8"); $str = uniqid(md5(microtime(true) ...

  5. SharePoint 2013 workflow cannot start automatically when you logged in site as a system account

    I have created one simple workflow on custom list using SharePoint designer 2013.While designing wor ...

  6. 【转】如何读懂Oracle文档中的语法图

    转自:http://blog.itpub.net/22990797/viewspace-750157/ Oracle文档中用到了两种表达语法的方法,语法图和BNF. BNF, Backus-Naur ...

  7. ChemOffice Professional 16.0新增了哪些功能

    ChemOffice Professional 16.0是为终极化学和生物组件设计,可满足化学家和生物学家的需求.ChemOffice Professional帮助科学家有效地检索数据库,包括SciF ...

  8. 详解MathType快捷键使用技巧

    巧妙使用MathType数学公式编辑器可加快数学符号的录入速度和效率,这将节约大量的时间成本,本教程将详解MathType快捷键使用的诸多技巧. 在MathType界面,将鼠标轻轻的放在某个符号上,在 ...

  9. 再不学会这些技巧,你就OUT了!

    俗话说的好:技多不压身!这句话真是一点都没错,尤其是在21世纪的今天,作为老师的你,如果不会使用下面所要说的这款神器,恐怕你就像玩游戏一样,要被get out!那到底是什么呢?它就是现在正在全国初高中 ...

  10. 0060 Spring MVC的数据类型转换--ConversionService--局部PropertyEditor--全局WebBindingInitializer

    浏览器向服务器提交的数据,多是字符串形式,而有些时候,浏览器需要Date.Integer等类型的数据,这时候就需要数据类型的转换器 使用Spring的ConversionService及转换器接口 下 ...