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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- 嵌入式开发之qt socket--- qt 封装的socket demo
http://wuyuans.com/2013/03/qt-socket/ http://blog.chinaunix.net/uid-22480862-id-388253.html
- Expression Tree上手指南 (一)
大家可能都知道Expression Tree是.NET 3.5引入的新增功能.不少朋友们已经听说过这一特性,但还没来得及了解.看看博客园里的老赵等诸多牛人,将Expression Tree玩得眼花缭乱 ...
- 关于PHP反射
本文实例讲述了PHP反射机制原理与用法.分享给大家供大家参考,具体如下: 反射 面向对象编程中对象被赋予了自省的能力,而这个自省的过程就是反射. 反射,直观理解就是根据到达地找到出发地和来源.比如,一 ...
- Node-Webkit - package.json - 参数设置
必填: main :(string)APP的主入口,指定一个html文件,如:main:"index.htm". name :(string)APP的名称,必须具有唯一性. 例子: ...
- js自动补全
<!doctype html> <html> <style> body { margin-left: 0px; margin-top: 0px; margin-ri ...
- Cocos2d-x 3.1 环境搭建和创建project
Cocos2d-x 3.x改版了非常多,之前搭过一次环境,可是没截图.这次趁着重装电脑,一边搭建一边截图.此博文仅仅是为了记录而不是为了教学,所以很多其它讲的是搭建过程.本文基本上參考这篇博客:htt ...
- Docker dubbo 服务注册
vim run.sh #!/bin/baship=`ifconfig eth0 |grep "inet"|awk '{print $2}'`hn=dubbo-service-pro ...
- mysql怎么在已建好的表中添加自增序列
alter table 表明 change id id int not null auto_increment unique;
- python基础15 ---面像对象的程序设计
面向对象的程序设计 一.面向对象的程序设计简介 1.面向对象程序设计的由来. 我们之前虽然学习过了面向过程的程序,它的核心是面向过程,一步一步的设计好了的流程,虽然极大的降低了程序的复杂度,但是一个设 ...
- (转载)DataTable与List<T>相互转换
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...