hdu----(1671)Phone List(Trie带标签)
Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10837 Accepted Submission(s): 3735
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.
3
911
97625999
91125426
5
113
12340
123440
12345
98346
YES
/*hdu 1671 ×ÖµäÊ÷*/
#define LOCAL
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
struct Trie
{
struct Trie *next[];
bool tail;
};
char str[][];
void in_Trie(char *s,Trie *root)
{
Trie *cur=root,*newcur;
for(int i=;s[i]!='\0';i++)
{
if(cur->next[s[i]-'']==NULL)
{
newcur=new Trie; //(Trie*)malloc(sizeof(sizeof(Trie)));
for(int j=;j<=;j++)
newcur->next[j]=NULL;
newcur->tail=;
cur->next[s[i]-'']=newcur;
}
cur=cur->next[s[i]-''];
}
cur->tail=;
}
bool query(char *s,Trie *root)
{
int i=;
Trie *cur=root;
for(i=;s[i];i++){
if(cur->tail==&&cur->next[s[i]-'']!=NULL)
cur=cur->next[s[i]-''];
else break;
}
if(s[i]!='\0') return ;
return ;
}
void dele(Trie *root)
{
for(int i= ; i<= ; i++ )
if(root->next[i]!=NULL)
dele(root->next[i]);
// free(root);
delete root;
}
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
int t,i,n;
Trie *root;
scanf("%d",&t);
while(t--)
{
root = new Trie ;
for(int j=;j<=;j++)
root->next[j]=NULL;
root->tail=; scanf("%d",&n);
for(i=;i<n;i++){
scanf("%s",str[i]);
in_Trie(str[i],root);
} for(i=;i<n;i++)
if(query(str[i],root)!=)
break;
dele(root);
if(i>=n)
printf("YES\n");
else
printf("NO\n");
}
return ;
}
hdu----(1671)Phone List(Trie带标签)的更多相关文章
- HDU 1671 Phone List (Trie·数组实现)
链接:http://blog.csdn.net/acvay/article/details/47089657 题意 给你一组电话号码 判断其中是否有某个电话是另一个电话的前缀 字典树的基础应用 ...
- hdu 1671 Phone List (Trie树)
简单的字典树应用,在建树的时候判断就行了. 需要注意的语法: 在使用malloc和free来处理动态内存的时候,仅仅是释放了这个对象所占的内存,而不会调用这个对象的析构函数:使用new和delete就 ...
- [hdu 1671] Phone List - Trie
Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...
- hdu 1671&& poj 3630 (trie 树应用)
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25280 Accepted: 7678 Descr ...
- HDU 1671 Phone List(Trie的应用与内存释放)
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 1671 Phone List 字典树
// hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...
- [TFRecord格式数据]利用TFRecords存储与读取带标签的图片
利用TFRecords存储与读取带标签的图片 原创文章,转载请注明出处~ 觉得有用的话,欢迎一起讨论相互学习~Follow Me TFRecords其实是一种二进制文件,虽然它不如其他格式好理解,但是 ...
- tp5.带标签的缓存 创建和清除 测试
原文:http://www.upwqy.com/details/24.html 测试设置了标签的缓存的获取方式 和清除标签缓存. 有时候我们可能会对同类型的一些数据做统一缓存.和统一清除更新处理. 那 ...
- 带标签的循环语句、switch
今天继续更新,控制流程的剩余部分内容,带标签的循环语句中的continue/break 的使用方法,以及switch关键字的使用方法.例1:带标签的continue/break.package com ...
随机推荐
- 【转载】UML类图知识整理
原文:UML类图知识整理 UML类图 UML,进阶必备专业技能,看不懂UML就会看不懂那些优秀的资料. 这里简单整理 类之间的关系 泛化关系(generalization) 泛化(generalize ...
- 系统分区MBR、GPT
分区模式: ①MBR(主引导记录(Master Boot Record))分区:在驱动器最前端的一段引导扇区 缺点:主分区不超过4个,单个分区容量最大2TB 分区工具fdisk只能给硬盘做MBR分区, ...
- Sbt的使用初步和用sbt插件生成eclipse工程
以前一直是用maven去管理java项目,现在开始写scala项目了但是在scala-ide中去编译scala项目和sbt的区别一直没弄清楚受到文章:http://my.oschina.net/yjw ...
- 解决iOS内存泄露
文章很好,摘自:http://www.codeceo.com/article/xcode-instruments-ios-memory.html 虽然iOS 5.0版本之后加入了ARC机制,由于相互引 ...
- CSS笔记(十)position属性与定位
参考:http://www.w3school.com.cn/cssref/pr_class_position.asp position属性规定了元素的定位类型.任何元素都可定位,其中,绝对或固定元素会 ...
- [转]产品需求文档(PRD)的写作
产品需求对产品研发而言非常重要,写不好需求,后面的一切工作流程与活动都会受到影响.转载一篇文章,关于产品需求文档写作方面的,如下: 本文摘自(一个挺棒的医学方面专家):http://www.cnblo ...
- Vnc viewer与windows之间的复制粘贴
用VNC连接到Linux之后,最纠结的问题就是无法复制粘贴.其实很简单,在Linux里面,打开一个终端,然后输入命令: vncconfig 之后,会弹出一个窗口 不要关闭那个小窗口 之后,就可以愉快的 ...
- maven初试2
1.1.建立Hello项目 1.首先建立Hello项目,同时建立Maven约定的目录结构和pom.xml文件 Hello | --src | -----main | ----------java | ...
- 控制反转和spring在项目中可以带来的好处
Spring实例化Bean的三种方式分别是: 1,xml配置使用bean的类构造器 <bean id="personService" class="cn.servi ...
- 深入浅出RxJava(一:基础篇)
RxJava正在Android开发者中变的越来越流行.唯一的问题就是上手不容易,尤其是大部分人之前都是使用命令式编程语言.但是一旦你弄明白了,你就会发现RxJava真是太棒了. 这里仅仅是帮助你了解R ...