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. linux -- 终端执行可执行文件

    有一个可执行文件:/media/home/1.sh 1.首先,得有权限 (1)如果已经是root用户(#),则不用做什么 (2)如果不是 $ sudo su pass your password:(这 ...

  2. MySQL无法远程连接解决方案

    1.查看/etc/mysql/my.cnf配置文件是否只允许本地连接 注释配置:#bind-address = 127.0.0.1,重启MySQL Server 2.防火墙(我用的是iptables) ...

  3. EF中修改对象的值的问题。。。(字段超级多的时候)

    一般EF中修改单个对象的值,我是这样处理的. 如:DBEntities db=new DBEntities(); student stu = db.student.firstOrdefault(m=& ...

  4. PHP json_decode 无法解析特殊问号字符

    在通过别人接口请求信息的时候,偶尔会遇到由于部分字符,如以下情况,则通过json_decode是会返回null的 但是这种情况通常不是由于整体编码的问题,因为在解析的时候就是以utf-8的编码解析的 ...

  5. iOS 图片切片的简单实现

    图片切片就是将一张图片按一定比例切分,中间部分系统自动填充空白, 这样在文本多行输入的时候,将具有特定形状的图片作为背景, 即使文本行数很多,也不会使图片走形. //即使你按5/5分,还是3/7分系统 ...

  6. android 沉浸式状态栏(像ios那样的状态栏与应用统一颜色样式)

    这个特性是andorid4.4支持的,最少要api19才干够使用.以下介绍一下使用的方法,很得简单: 添加一个demo源代码: https://github.com/ws123/StatusDemo ...

  7. MathType公式编辑器快捷键操作

    快捷键操作是最常见的操作方式,MathType软件系统提供大量的快捷键操作供用户使用.使用MathType公式编辑器快捷键操作可节省大量的操作的时间,本教程将详解MathType快捷键操作. 放大或缩 ...

  8. 【matlab】图像去噪的代码测试

    %% 自己设置频域的滤波窗口 girl=imread('F:\Users\*****\Pictures\CGS_stripe1.bmp'); girl=rgb2gray(girl); girl=im2 ...

  9. 关于Java 枚举类型的自定义属性

    package com.cpic.test;/** * 关于枚举类型自定义属性 * */public enum Provious { ANHUI("皖", 1),BAIJING(& ...

  10. [kfaka] Apache Kafka:下一代分布式消息系统

    简介 Apache Kafka是分布式发布-订阅消息系统.它最初由LinkedIn公司开发,之后成为Apache项目的一部分.Kafka是一种快速.可扩展的.设计内在就是分布式的,分区的和可复制的提交 ...