poj3630 Phone List【Trie树】
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树】的更多相关文章
- poj3630 Phone List (trie树模板题)
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26328 Accepted: 7938 Descr ...
- HihoCoder第二周与POJ3630:Trie树的建立
这又是两道一样的题,都是建立trie树的过程. HihoCoder第二周: 这里其实逻辑都很简单,主要在于数据结构struct的使用. #include <iostream> #inclu ...
- POJ3630——简单Trie树
这个题的意思是说,给出一些字符串,判断是否有字符串是另一个字符串的前缀,当然可以用排序水过,不过这个题拿来练习一下Trie树不错. 这个题在poj的discuss上好多人说必须要静态建树,估计都是用了 ...
- 蒟蒻的trie树专题
POJ 3630 Phone List: 模板 ///meek #include<bits/stdc++.h> using namespace std; using namespace s ...
- 基于trie树做一个ac自动机
基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...
- 基于trie树的具有联想功能的文本编辑器
之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...
- hihocoder-1014 Trie树
hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...
- 洛谷P2412 查单词 [trie树 RMQ]
题目背景 滚粗了的HansBug在收拾旧英语书,然而他发现了什么奇妙的东西. 题目描述 udp2.T3如果遇到相同的字符串,输出后面的 蒟蒻HansBug在一本英语书里面找到了一个单词表,包含N个单词 ...
- 通过trie树实现单词自动补全
/** * 实现单词补全功能 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #incl ...
随机推荐
- udp编程中,一次能发送多少个bytes为好?
在进行UDP编程的时候,我们最容易想到的问题就是,一次发送多少bytes好? 当然,这个没有唯一答案,相对于不同的系统,不同的要求,其得到的答案是不一样的,我这里仅对 像ICQ一类的发送聊天消息 ...
- VC++使用CSocket发送HTTP Request时需要注意发送数据的编码格式
VS2010以及更高版本中新建的MFC项目字符集默认是Unicode,CString创建的字符串默认是Unicode. 使用CSocket时,若以CString组织需要发送的HTTP Head时,那么 ...
- 转载:【原译】Erlang性能的八个误区(Efficiency Guide)
转自:http://www.cnblogs.com/futuredo/archive/2012/10/16/2725770.html The Eight Myths of Erlang Perform ...
- Fastqc 碱基质量分布图
横坐标代表每个每个碱基的位置,反映了读长信息,比如测序的读长为150bp,横坐标就是1到150: 纵坐标代表碱基质量值, 图中的箱线图代表在每个位置上所有碱基的质量值分布, 中间的红线代表的是中位数 ...
- 最有价值的50道java面试题 适用于准入职Java程序员
下面的内容是对网上原有的Java面试题集及答案进行了全面修订之后给出的负责任的题目和答案,原来的题目中有很多重复题目和无价值的题目,还有不少的参考答案也是错误的,修改后的Java面试题集参照了JDK最 ...
- Xcode 5.0 编译低版本app
Xcode 5.0 默认的编译环境是iOS7,编译出来的app,安装到iOS7.0版本以上的手机上,会表现出iOS7.0的风格.兼容不太好的应用,布局上可能会因此乱八七糟. 如果还不想让app升级到i ...
- 【转】struts2.5框架使用通配符指定方法(常见错误)
在学习struts框架时经常会使用到通配符调用方法,如下: <package name="shop" namespace="/" extends=&quo ...
- c :函数指针具体解释
在研究opencv源码的过程中.处处可见到函数指针,于是翻出来谭浩强的<C程序设计>把函数指针这一块内容再补一补! 1 定义 数据类型 (*指针变量名)(參数表); 注: 数据类型是指的函 ...
- 字符串池化 python
前言 在 Python 中经常通过内存池化技术来提高其性能,那么问题来了,在什么情况下会池化呢? 让我们通过几个例子进行一下理解一下. 预备知识 在查看例子之前,首先要提 python 中的一个函数 ...
- 设计模式工具:UML基础
类图 矩形框 -类Class 第一层 ...