[ACM] hdu 1671 Phone List (字典树)
Phone List
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.
is a sequence of at most ten digits.
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
NO
YES
解题思路:
推断输入的串中是否存在某个串是另外串的前缀。
建立字典树,关键是设立某个串结尾的标志,即在哪个字母结尾。
要推断是否存在前缀要考虑两种情况。一是当前输入的串是否是曾经输入的串的前缀,而是曾经输入的串是否是当前输入的串的前缀。前一种情况在查找该串时,会从第一个字母查找到最后一个字母,中间不返回不论什么值,说明当前的串是曾经的前缀,后一种情况。当在查找当前串的时候遇到曾经串结束的标志。则说明曾经串是当前串的前缀。
代码中结束的标志为保存串中最后一个字母的那个节点的cnt值为-1.
如图:
代码:
#include <iostream>
#include <malloc.h>
#include <algorithm>
#include <string.h>
#include <stdio.h>
using namespace std;
const int maxn=10;
bool ok;
char str[12];
int t,n; struct Trie
{
int cnt;
Trie *next[maxn];
}; Trie *root; void CreateTrie(char *str)
{
int len=strlen(str);
Trie*p=root,*q;
for(int i=0;i<len;i++)
{
int id = str[i]-'0';
if(p->next[id] == NULL)
{
q = (Trie *)malloc(sizeof(Trie));
q->cnt = 1;
for(int j=0; j<maxn; ++j)
q->next[j] = NULL;
p->next[id] = q;
p = p->next[id];
}
else
{
p = p->next[id];
}
}
p->cnt=-1;//串末尾的标志
} int findTrie(char *str)
{
int len=strlen(str);
Trie *p=root;
for(int i=0;i<len;i++)
{
int id=str[i]-'0';
if(p->next[id]==NULL)
return 0;//没有建过树
if(p->next[id]->cnt==-1)
return -1;//曾经串是当前串的前缀
p=p->next[id];
}
return -1;//当前串是曾经串的前缀
} void release(Trie *root)//释放空间
{
for(int i=0;i<maxn;i++)
{
if(root->next[i])
release(root->next[i]);
}
free(root);
}
int main()
{
scanf("%d",&t);
while(t--)
{
ok=1;
root=(Trie*)malloc(sizeof(Trie));//root为指针类型,须要申请空间
for(int i=0; i<10; ++i)
root->next[i] = NULL;
scanf("%d",&n);
while(n--)
{
scanf("%s",str);
if(findTrie(str)==-1)
ok=0;//有前缀
if(!ok)
continue;//有前缀,后面的就不用建树了
CreateTrie(str);
}
if(ok)
printf("YES\n");
else
printf("NO\n");
release(root);
}
return 0;
}
[ACM] hdu 1671 Phone List (字典树)的更多相关文章
- hdu 1671 Phone List 字典树
// hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...
- hdu 1671 Phone List 字典树模板
Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...
- ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)
Description We all use cell phone today. And we must be familiar with the intelligent English input ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- HDU 5536 Chip Factory 字典树
Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- HDU 1298 T9(字典树+dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...
- hdu 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU 1298 T9【字典树增加||查询】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1298 T9 Time Limit: 2000/1000 MS (Java/Others) Memo ...
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
随机推荐
- 用Qemu模拟vexpress-a9 (七) --- 嵌入式设备上安装telnet服务
转载: http://blog.csdn.net/liuqz2009/article/details/6921789 Telnet协议是登陆远程网 络主机最简单的方法之一,只是安全性非常低.对targ ...
- hadoop函数说明图
- [部署Mantis]用Administrator注册新用户时设置密码
伤不起的Mantis邮箱配置,在新的Mantis配置里面默认通过接收激活邮件来设定密码. 如果你Mantis邮箱配置OK的话一切OK,遇到我这样死活配不成功,网络上大神们众说纷纭,一一参照,无奈死伤无 ...
- [GIt] 团队工作效率分析工具gitstats
copy : http://www.cnblogs.com/ToDoToTry/p/4311637.html 如果你是团队领导,关心团队的开发效率和工作激情:如果你是开源软件开发者,维护者某个repo ...
- 跟我学AngularJs:AngularJs入门及第一个实例
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:主要给大家介绍了AngularJs及其特性,并以3个实例来做说明. 本教程使用Angul ...
- MICS:副本和纠删码混合存储系统
摘要 云存储系统的三个指标: 高可靠性,低存储开销,高读写性能. 这三个指标是没有办法同一时候满足的,许多时候须要进行tradeoff. 副本系统和纠删码是两种在存储系统中广泛使用的策略,它们在保证高 ...
- Objective-C:NSDectionary字典的常见操作
NSDectionary字典:它是一个存储键值的容器,每一个键key都对应着一个值value,可以通过键key一次性找到目标值value,这是一个比较好的存储器,相比于数组而言,它明显提高了查询效率. ...
- spring cloud-给Eureka Server加上安全的用户认证
前言 在前面的一篇文章中 spring cloud中启动Eureka Server 我们启动了Eureka Server,然后在浏览器中输入http://localhost:8761/后,直接回车,就 ...
- 第三章:ES分词简单说明
1. Index_Analyzer为索引时使用的分词器,search_Analyzer为搜索时使用的分词器 这个索引,对应的数据是下图: 数据格式是用%分隔的年份,"index_analyz ...
- 思维导图软件xmind和mindmanager哪个更好
思维导图是一种将放射性思考具体化的方法,可以将人们的创造性思维及时捕捉并呈现,目前便捷的网络为人们带来了众多的思维导图软件,而在这些软件中只有亲身实践体验过,才能知道到底思维导图哪个好,哪个又适合自己 ...