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 ...
随机推荐
- android 创建 xml文件
android创建xml文件的方法. 要操作android的外部存储,所以要在AndroidManifest.xml文件中添加权限. <uses-permission android:name= ...
- 谷歌修复了 FFmpeg 中上千个 bug
谷歌在科技业界中几乎每天都会创造出新闻素材,它的触手涉及到了生活中的多个领域.最近谷歌将其Google +社交网络与邮件服务Gmail相结合.然而今天谷歌宣布他们修复了FFmpeg的上千个bug. ...
- (转)深入解析SendMessage、PostMessage
转自:http://blog.csdn.net/xt_xiaotian/article/details/2778689 本文将使用C++语言,在MFC框架的配合下给出PostMessage.S ...
- R语言低级绘图函数-points
points 用来在一张图表上添加点,指定好对应的x和y坐标,就可以添加不同形状,颜色的点了: 基本用法: 通过x和y设置点的坐标 plot(1:5, 1:5, xlim = c(0,6), ylim ...
- 解决 Comparison method violates its general contract!
问题:Comparison method violates its general contract!报错 Collections.sort(list, new Comparator<Integ ...
- 五步让你玩转CocoaPods
1 安装和升级 $ sudo gem install cocoapods $ pod setup 2 更换为taobao的源 $ gem sources -r https://rubygems.or ...
- 创建并调用 DLL(1)
//通过 DLL Wizard 建立: library TestDLL; uses SysUtils, Classes, Dialogs; {$R *.res} //建立过程 proced ...
- C#中汉字轻松得到拼音全文类
public class chs2py { ,-,-,-,-,-,-,-,-,-,-,-,-,-, -,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-, -,-,-,-,-,-,-,-,- ...
- exp/imp与expdp/impdp区别
在平常备库和数据库迁移的时候,当遇到大的数据库的时候在用exp的时候往往是需要好几个小时,耗费大量时间.oracle10g以后可以用expdp来导出数据库花费的时间要远小于exp花费的时间,而且文件也 ...
- surfaceView和View的区别
概念:view在UI线程去更新自己:而SurfaceView则在一个子线程中去更新自己 surfaceView是在一个新起的单独线程中可以重新绘制画面,而View必须在UI的主线程中更新画面 在UI的 ...