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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- How can I detect multiple logins into a Django web application from different locations?
1) Install django-tracking (thankyou for that tip Van Gale Google Maps + GeoIP is amazing!) 2) Add t ...
- 用android studio创建第一个安卓程序加载html5 页面
前言 软件版本:android studio v1.0正式版,由于v0.x以来软件变化一直比较大,很多问题搜索的解决方案也都是v0.x版本时代的,故首先声明一下版本. 动机:由于工作中需要对移动端软件 ...
- C语言基础知识【程序结构】
C 程序结构1.C 程序主要包括以下部分:预处理器指令函数变量语句 & 表达式注释2.#include <stdio.h> int main(){ /* 我的第一个 C 程序 ...
- python 正則表達式推断邮箱格式是否正确
import re def validateEmail(email): if len(email) > 7: if re.match("^.+\\@(\\[?) ...
- Manager模块 队列 管道 进程池
Manager模块 作用: 多进程共享变量. Manager的字典类型: 如果value是简单类型,比如int,可以直接赋值给共享变量,并可以后续直接修改 如果value是复杂类型 ,比如list, ...
- poj3708(公式化简+大数进制装换+线性同余方程组)
刚看到这个题目,有点被吓到,毕竟自己这么弱. 分析了很久,然后发现m,k都可以唯一的用d进制表示.也就是用一个ai,和很多个bi唯一构成. 这点就是解题的关键了. 之后可以发现每次调用函数f(x),相 ...
- webview300毫秒点击问题
http://www.jshacker.com/note/3585 http://blog.csdn.net/zfy865628361/article/details/49512095 http:// ...
- 【BZOJ2806】[Ctsc2012]Cheat 广义后缀自动机+二分+单调队列优化DP
[BZOJ2806][Ctsc2012]Cheat Description Input 第一行两个整数N,M表示待检查的作文数量,和小强的标准作文库的行数接下来M行的01串,表示标准作文库接下来N行的 ...
- iOS源代码管理svn
01. SVN介绍 SVN 是集中式源代码管理工具 概念: 1> Repository 代码仓库,保存代码的仓库 2> Server 服务器,保存所有版本的代码仓库 3&g ...
- 京东android面试题(2018 顶级互联网公司面试题系列)
以下来自于北京的一个兄弟的面试题 1.静态内部类和非静态内部类有什么区别 2.谈谈你对java多态的理解 3.如何开启线程,run和runnable有什么区别 4.线程池的好处 5.说一下你知 ...