HDU 1671 Phone List(Trie的应用与内存释放)
Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18217 Accepted Submission(s): 6120
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.
题目链接:HDU 1671
嗯还是一道简单的Trie,但是对于内存的要求比较高= =不管是malloc或new出来的,用完一定要释放不然……MLE数次。说是Trie其实是学习一下如何进行有效率地释放……
注意题目中可能给的单词顺序不同会出现两种存在前缀的情况
例1、输入911 后输入911000,这个比较简单,逐一插入911000的时候若节点存在则检查是否此时的节点的flag为true即这个节点是一个结尾点。
例2、输入911000 后输入911,办法有很多,我是用一个any来记录后面的911插入时是否出现过未出现的字母,显然在节点不存在new的时候any就要变成1了。
代码:
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
const int N=10;
const int M=15;
struct Trie
{
Trie *nxt[N];
bool flag;
Trie()
{
for (int i=0; i<N; ++i)
nxt[i]=NULL;
flag=false;
}
};
Trie *L;
bool update(char s[])
{
int i,len=strlen(s);
int indx;
bool is_end=false;
bool any=false;//至少存在一个没出现过的单词
Trie *cur=L;
for (i=0; i<len; ++i)
{
indx=s[i]-'0';
if(!cur->nxt[indx])
{
Trie *one=new Trie();
any=true;
cur->nxt[indx]=one;
cur=one;
}
else
{
cur=cur->nxt[indx];
if(cur->flag)//过程中遇到结尾单词,前缀存在
is_end=true;
}
}
if(!any)
is_end=true;
cur->flag=true;
return is_end;
}
void deleTrie(Trie *L)
{
Trie *cur=L;
for (int i=0; i<N; ++i)
if(cur->nxt[i])
deleTrie(cur->nxt[i]);
delete cur;
}
char s[M];
int main(void)
{
int tcase,n;
scanf("%d",&tcase);
while (tcase--)
{
bool flag=true;
L=new Trie();
scanf("%d",&n);
while (n--)
{
scanf("%s",s);
if(flag)
{
if(update(s))
flag=false;
}
}
puts(flag?"YES":"NO");
deleTrie(L);
}
return 0;
}
HDU 1671 Phone List(Trie的应用与内存释放)的更多相关文章
- hdu 1671 Phone List (Trie树)
简单的字典树应用,在建树的时候判断就行了. 需要注意的语法: 在使用malloc和free来处理动态内存的时候,仅仅是释放了这个对象所占的内存,而不会调用这个对象的析构函数:使用new和delete就 ...
- HDU 1671 Phone List (Trie·数组实现)
链接:http://blog.csdn.net/acvay/article/details/47089657 题意 给你一组电话号码 判断其中是否有某个电话是另一个电话的前缀 字典树的基础应用 ...
- [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 字典树
// hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...
- hdu 4825 Xor Sum(trie+贪心)
hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...
- HDU 1671 Phone List (Trie)
pid=1671">Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 1671 (字典树统计是否有前缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...
- POJ 3630 , HDU 1671 Phone List - from lanshui_Yang
这道题也是一道找前缀的问题,很自然地要用到Trie树,但是如果用动态Trie树(即用指针开辟内存)的话,虽然在HDU上可以过(可能是HDU的数据比较水),但在POJ上会TLE , 所以这道题只能用静态 ...
随机推荐
- 安装绿色版mysql
#修改my.ini basedir = "D:\tools\mysql-5.7.13-winx64" datadir = "D:\tools\mysql-5.7.13-w ...
- 转圈游戏(codevs 3285)
题目描述 Description n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 ...
- Txx考试(codevs 2894)
2894 Txx考试 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description Txx是一个成绩很差的人,考 ...
- c++ 继承 虚函数与多态性 重载 覆盖 隐藏
http://blog.csdn.net/lushujun2011/article/details/6827555 2011.9.27 1) 定义一个对象时,就调用了构造函数.如果一个类中没有定义任何 ...
- [转]ThreadPoolExecutor线程池的分析和使用
1. 引言 合理利用线程池能够带来三个好处. 第一:降低资源消耗.通过重复利用已创建的线程降低线程创建和销毁造成的消耗. 第二:提高响应速度.当任务到达时,任务可以不需要等到线程创建就能立即执行. 第 ...
- Webservice加上SoapHeader验证方式
提供一种基于SoapHeader的自定义验证方式,代码如下: public class MySoapHeader : System.Web.Services.Protocols.SoapHeader ...
- JavaEE路径陷阱之getRealPath
转自:http://blog.csdn.net/shendl/article/details/1427637 JavaEE路径陷阱之getRealPath 本文是<Java路径问题最终解 ...
- Bean的前身今世&处理器&Aware
出生: <bean>......</bean> 幼年:BeanDefinition bean的接口定义 小学:GenericBeanDefinition 基础的bean映射 ...
- mvc-3模型和数据(1)
MVC和命名空间 var User = function(atts) { this.attribute = atts || {}; } //和具体user相关的方法 User.prototype.de ...
- AngularJS学习笔记一:简单入门
阿里云网站的前端是AngularJS实现的. 先下载AngularJS的开发工具包,我下载的angular-1.4.0. 在合适位置引入js文件: <script src="angul ...