[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 ...
随机推荐
- MVC为用户创建专属文件夹
假设需要为用户创建专属文件夹,文件夹名为用户名,并且需要根据用户类型在不同的文件夹下创建目标文件夹. 在F盘创建"Users"文件夹,在其中创建"Gold"文件 ...
- linux 查找文件命令
find -name 文件名 在当前目录下查找 find -name nginx.conf
- symbol(s) not found for architecture armv7
Undefined symbols for architecture i386: “_OBJC_CLASS_$_XXX”, referenced from: objc-class-ref in XXX ...
- codeforces 444 C. DZY Loves Colors(线段树)
题目大意: 1 l r x操作 讲 [l,r]上的节点涂成x颜色,而且每一个节点的值都加上 |y-x| y为涂之前的颜色 2 l r 操作,求出[l,r]上的和. 思路分析: 假设一个区间为同样的颜 ...
- Tomcat6内存不足问题及解决方法
1.Tomcat默认可以使用的内存为128MB,在较大型的应用项目中,这点内存是不够的,有可能导致系统无法运行.常见的问题是报Tomcat内存溢出错误,Out of Memory(系统内存不足)的异常 ...
- 常用的快速Web原型图设计工具
做产品原型是非常重要的一个环节,做产品原型就会用使用各式各样的工具.在PM朋友们的推荐下使用了很多各种各样的软件,当然选择一款真正适合自己的工具也是很重要,在这里就把我使用过的工具都介绍一下. 主要有 ...
- MySql_34道经典Sql试题
MySql_34道经典Sql试题 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/xiaouncle/article/details/799390 ...
- strstr实现
// strstr.c查找完全匹配的子字符串 #include<stdio.h> #include<string.h> char *my_strstr(const char * ...
- TRIZ理论的进化法则分析(TRIZ学习笔记)
人们在创新和完好系统的过程能够遵循一定的规律(或者叫法则).从而降低创新和完好系统过程中的试错成本,以下就TRIZ的八大进化原则来进行说明(这个八大法则是前人们的总结,我这里当然会增加我的理解). 我 ...
- [leetcode]Convert Sorted List to Binary Search Tree @ Python
原题地址:http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题意:将一条排序好的链表转换为二叉查找树 ...