Phone List

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18217    Accepted Submission(s): 6120

Problem Description
Given 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.

 
Input
The 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.
 
Output
For each test case, output “YES” if the list is consistent, or “NO” otherwise.
 
Sample Input
2
 
3
911
97625999
91125426
 
5
113
12340
123440
12345
98346
 
Sample Output
NO
YES
 

题目链接: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的应用与内存释放)的更多相关文章

  1. hdu 1671 Phone List (Trie树)

    简单的字典树应用,在建树的时候判断就行了. 需要注意的语法: 在使用malloc和free来处理动态内存的时候,仅仅是释放了这个对象所占的内存,而不会调用这个对象的析构函数:使用new和delete就 ...

  2. HDU 1671 Phone List (Trie·数组实现)

    链接:http://blog.csdn.net/acvay/article/details/47089657 题意  给你一组电话号码  判断其中是否有某个电话是另一个电话的前缀 字典树的基础应用   ...

  3. [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 ...

  4. hdu 1671&& poj 3630 (trie 树应用)

    Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25280   Accepted: 7678 Descr ...

  5. hdu 1671 Phone List 字典树

    // hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...

  6. hdu 4825 Xor Sum(trie+贪心)

    hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...

  7. HDU 1671 Phone List (Trie)

    pid=1671">Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  8. HDU 1671 (字典树统计是否有前缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...

  9. POJ 3630 , HDU 1671 Phone List - from lanshui_Yang

    这道题也是一道找前缀的问题,很自然地要用到Trie树,但是如果用动态Trie树(即用指针开辟内存)的话,虽然在HDU上可以过(可能是HDU的数据比较水),但在POJ上会TLE , 所以这道题只能用静态 ...

随机推荐

  1. 学习 BigInteger

    以下是摘抄与其他人的: JAVA之BigInteger 用Java来处理高精度问题,相信对很多ACMer来说都是一件很happy的事,简单易懂.用Java刷了一些题,感觉Java还不错,在处理高精度和 ...

  2. mysql中char,varchar,text区别总结

    具体对这三种类型的说明不做阐述可以查看mysql帮助文档. char的总结:      char最大长度是255字符,注意是字符数和字符集没关系.可以有默认值,尾部有空格会被截断.varchar的总结 ...

  3. python file.tell() 在windows下需要注意的地方

    顺便记一下,'rba'模式是非法的...

  4. poj 2245 水题

    求组合数,dfs即可 #include<cstdio> #include<iostream> #include<algorithm> #include<cst ...

  5. How to enable logging

    转自:https://www.chromium.org/for-testers/enable-logging How to enable logging To enable logging, laun ...

  6. Rational Software Architect V8.5.1安装

    转自:http://blog.sina.com.cn/s/blog_4a0238270101bupg.html IBM Rational Software Architect (RSA) 是 IBM ...

  7. 【vijos1659】河蟹王国 线段树<区间修改+区间最大值>

    描述 河蟹王国有一位河蟹国王,他的名字叫羊驼.河蟹王国富饶安定,人们和谐相处.有一天,羊驼国王心血来潮,想在一部分人中挑出最和谐的人.于是,羊驼国王将他的子民排成了一列(==!!b汗~好长呀).每个人 ...

  8. Android 返回键双击退出程序

    /** * 菜单.返回键响应 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == K ...

  9. 从数据库得到的结果集存放到List集合中

    一.业务阐述 在开发中查询的数据库结果集,既要连接数据库.执行数据库操作.关闭数据库,还要把结果集的记录人为的设置到自己封装的DAO中等一系列的重复代码. 本文主要是想解决:用户只需要得到数据库连接, ...

  10. 分享Kali Linux 2016.2第36周镜像虚拟机

    分享Kali Linux 2016.2第36周镜像虚拟机   9月9日,Kali Linux官方发布Kali Linux 2016.2周更新镜像.今天以64位镜像安装了一个虚拟机,分享给大家.该虚拟机 ...