HDU1671 字典树
Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18421 Accepted Submission(s): 6207
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:
1. Emergency 911
2. Alice 97 625 999
3. 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.
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.
//字典树的权值作为标记点,每输入一个号码,查找他每一位的val值有没有等于1的,如果没有就把他放入字典树他的最后一
//位的val值置为1,如果有说明与前面的某一个冲突。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAX=;
const int CON=;
int nod[MAX][CON],val[MAX];
int sz;
void init()
{
sz=;
memset(nod[],,sizeof(nod[]));
val[]=;
}
void insert(char s[])
{
int len=strlen(s);
int rt=;
for(int i=;i<len;i++)
{
int id=s[i]-'';
if(nod[rt][id]==)
{
memset(nod[sz],,sizeof(nod[sz]));
nod[rt][id]=sz;
val[sz++]=;
}
rt=nod[rt][id];
}
val[rt]=;
}
int search(char s[])
{
int len=strlen(s);
int rt=,cnt=;
for(int i=;i<len;i++)
{
int id=s[i]-'';
if(nod[rt][id]==)
return ;
rt=nod[rt][id];
if(val[rt]==)
return ;
}
}
int main()
{
char ch[];
int t,n,flag;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d",&n);
flag=;
for(int i=;i<=n;i++)
{
scanf("%s",ch);
if(flag) continue;
flag=search(ch);
insert(ch);
}
if(flag) printf("NO\n");
else printf("YES\n");
}
return ;
}
HDU1671 字典树的更多相关文章
- hdu1671字典树
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu1671 字典树记录前缀出现次数
题意: 给你一堆电话号,问你这些电话号后面有没有相互冲突的,冲突的条件是当前这个电话号是另一个电话号的前缀,比如有 123456789 123,那么这两个电话号就冲突了,直接输出NO. 思 ...
- Phone List HDU1671 字典树Trie
模板题...不过会爆内存,要小心 #include <iostream> #include <cstdio> #include <string.h> #pragma ...
- 3道入门字典树例题,以及模板【HDU1251/HDU1305/HDU1671】
HDU1251:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题目大意:求得以该字符串为前缀的数目,注意输入格式就行了. #include<std ...
- POJ3630/HDU-1671 Phone List,字典树静态建树!
Phone List POJ动态建树TLE了~~~ 题意:拨打某个电话时可能会因为和其他电话号码的前几位重复而导致错误,现在给出一张电话单,求是否有某个电话是其他电话的前缀.是则输出NO,否则输出YE ...
- hdu1671 Phone List [字典树 hash]
传送门 Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hihoCoder 1014trie树(字典树)
hihoCoder 1014 题目提示已经很清楚了~ 贴代码…… #include <iostream> #include <cstdio> #include <cstr ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
随机推荐
- Java重点识记
1.final修饰的类不能被继承,已经达到类层次中的最低层. 2.abstract修饰的类或成员方法,表明是抽象的,含有抽象方法的类必须说明是抽象类,必须派生出子类. 3.JAVA对逻辑与和逻辑或提供 ...
- C#深入浅出 继承(六)
这个标题写出来好长时间了,都没写内容,今天无论如何都得写完它,昨天写了一段,尼玛,电脑突然死机,重启之后啥都没了. 继承嘛,有人觉得很简单,但是里面还是有内容可以讲的,因为面向对象也就这么点内容,封装 ...
- ASP.NET 实现登陆验证
public class ValidModule : IHttpModule { /// <summary> /// 您将需要在网站的 Web.config 文件中配置此模块 /// 并向 ...
- can't debug windows service in win7 64bit
if encount below error: Solution: run the command “vsdiag_regwcf.exe -i” as admin in C:\Program File ...
- ThinkPHP 利用.htaccess文件的 Rewrite 规则隐藏URL中的 index.php
1.首先修改Apache的httpd.conf文件. 确认httpd.conf配置文件中加载了mod_rewrite.so 模块,加载的方法是去掉mod_rewrite.so前面的注释#号 讲http ...
- PHP中的错误处理、异常处理机制详解
在编写PHP程序时,错误处理是一个重要的部分.如果程序中缺少错误检测代码,那么看上去很不专业,也为安全风险敞开了大门 例: <?php $a = fopen('test.txt','r'); / ...
- php 跨域、跨子域,跨服务器读取session
1.跨子域和跨服务器解决方式 Session主要分两部分: 一个是Session数据,该数据默认情况下是存放在服务器的tmp文件下的,是以文件形式存在 另一个是标志着Session数据的Se ...
- am335x watchdog 设备出错
问题描述: am335x watchdog 设备节点打开失败. 如果是直接将omap_wdt 直接编译成uImage,这样会出现打开文件节点失败的情况. 如果单独编译成模块在后面文件系统内插入则不会. ...
- 如何查找本地的ip
输入cmd调出指令框然后输入ipconfig指令用快捷键ctrl右侧的键+R
- WIN7下VS2008生成Detours3.0
Detours是微软开发的一个函数库,可用于捕获系统API.在用其进行程序开发之前,得做一些准备工作: 一.下载Detours 在http://research.microsoft.com ...