poj3630 Phone List (trie树模板题)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 26328 | Accepted: 7938 | 
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
题意:就是问这些号码中是否存在一串号码是另一串号码的前缀,典型的trie树模板题;
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=100010;
char str[N][10];
int t,n,cnt,trie[N][20],flag,val[N];
void build_checktrie(char*s,int v)
{
int len=strlen(s),u=0;
for(int i=0;i<len;i++)
{
int num=s[i]-'0';
if(!trie[u][num])
{
for(int j=0;j<=10;j++)
{
trie[cnt][j]=0;
}
val[cnt]=0;
trie[u][num]=cnt++;
}
else
{
if(i==len-1)
{
flag=0;
}
if(val[trie[u][num]])
{
flag=0;
}
}
u=trie[u][num];
}
val[u]=v;
}
int main()
{
scanf("%d",&t);
while(t--)
{
memset(trie,0,sizeof(trie));
memset(val,0,sizeof(val));
cnt=1;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",str[i]);
}
flag=1;
for(int i=1;i<=n;i++)
{
build_checktrie(str[i],1);
if(!flag)break;
}
if(flag)printf("YES\n");
else printf("NO\n"); }
return 0;
}
poj3630 Phone List (trie树模板题)的更多相关文章
- HDU 1251  Trie树模板题
		
1.HDU 1251 统计难题 Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...
 - HDU 1251 统计难题 (Trie树模板题)
		
题目链接:点击打开链接 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单 ...
 - 835. 字符串统计(Trie树模板题)
		
维护一个字符串集合,支持两种操作: “I x”向集合中插入一个字符串x: “Q x”询问一个字符串在集合中出现了多少次. 共有N个操作,输入的字符串总长度不超过 105105,字符串仅包含小写英文字母 ...
 - hihocoder_1014: Trie树(Trie树模板题)
		
题目链接 #include<bits/stdc++.h> using namespace std; ; struct T { int num; T* next[]; T() { num=; ...
 - hihocoder 1014: Trie树(Trie树模板题)
		
题目链接 #include<bits/stdc++.h> using namespace std; ; struct T { int num; T* next[]; T() { num=; ...
 - Phone list(Trie树模板)
		
Phone List 共t组数据,给定n个长度不超过10的字符串,问其中是否存在两个数S,T,使得S是T的前缀. 存在则输出NO,不存在输出YES 输入样例#1: 2 3 911 97625999 9 ...
 - [AHOI 2009] 维护序列(线段树模板题)
		
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...
 - 字典树模板题(统计难题 HDU - 1251)
		
https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...
 - PAT甲级题解-1066. Root of AVL Tree (25)-AVL树模板题
		
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6803291.html特别不喜欢那些随便转载别人的原创文章又不给 ...
 
随机推荐
- Swift_4_闭包(Blocks)
			
import Foundation println("Hello, World!") var arr = [1,2,4,6,74,2] func hasClosure(list:[ ...
 - I2C驱动详解
			
I2C讲解: 在JZ2440开发板上,I2C是由两条数据线构成的SCL,SDA:SCL作为时钟总线,SDA作为数据总线:两条线上可挂载I2C设备,如:AT24C08 两条线连接ARM9 I2C控制器, ...
 - 【BZOJ3926】[Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机
			
[BZOJ3926][Zjoi2015]诸神眷顾的幻想乡 Description 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽香的2600岁生日,无数幽香的粉丝到了幽香家门前的太阳花田上来为幽香庆祝 ...
 - What is the difference between iterations and epochs in Convolution neural networks?
			
https://stats.stackexchange.com/questions/164876/tradeoff-batch-size-vs-number-of-iterations-to-trai ...
 - [ubuntu]安装adobe air
			
修改安装文件为可执行权限: sudo ./AdobeAIRInstaller.bin 提示错误: <code> Adobe AIR could not be installed. Inst ...
 - 我的Android进阶之旅------>如何为ListView组件加上快速滑块以及修改快速滑块图像
			
使用布局文件需要将android:fastScrollEnabled="true" ,如下代码所示: <ListView android:id="@+id/list ...
 - linux c编程:线程创建
			
前面章节中介绍了进程.从这一章开始介绍线程.进程和线程的差别是什么呢: 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立单位. 线程是进程的一个实 ...
 - Linux改动hostname的两个办法
			
假设你想把主机名改为 linux的话.两中方法: 1. # hostname linux 这样改动了以后马上生效.可是重新启动后就没了 2. # vi /etc/sysconfig/network 改 ...
 - WebApp页面开发小结
			
一 背景 公司需要开发一个web页面,需要支持主流android和ios手机,采用web页面好处是一个页面,在不同平台之间都可以用,节省成本,基本html.js和css大家也都熟悉.但是对 ...
 - 用cocos2d-html5做的消除类游戏《英雄爱消除》——概述
			
在bbs.html5china.com论坛学习了MV和老马的小熊蘑菇后我也自己模仿他们做了这样子一个游戏,权当技术交流学习,现在附上游戏截图和源码. 游戏截图: 1.系统菜单界面: 2.游戏界面 3. ...