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. Python Elasticsearch api

    描述:ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.下面介绍了利用Python API接口进行数据查询,方便 ...

  2. BroadcastReceiver study

    BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播. 在Android系统中,广播体现在方方面面,例如当开机完成后系统会产生一条广播,接收到这 ...

  3. C++ friend

    如果类A希望类B可以访问它的私有成员, 可以把类B设置为友元类. // 类A,希望把私有成员公开给类B class A {     friend class B;// 把B设置为友元类 public: ...

  4. adb logcat 命令

    转自:http://blog.csdn.net/tumuzhuanjia/article/details/39555445 1. 解析 adb logcat 的帮助信息 在命令行中输入 adb log ...

  5. CodeIgniter报错: You must use the "set" method to update an entry

    I'm using codeigniter/datamapper to develop an inviocing application and I'm getting an error that i ...

  6. C#实现UTC时间与Datetime转换

    为了便于传输,通信过程中传输的都是:当前时间跟标准时间相隔的秒数,并且是以16进制字节的形式传输的. public double ConvertDateTimeInt(System.DateTime ...

  7. 用JAXP的dom方式解析XML文件

    用JAXP的dom方式解析XML文件,实现增删改查操作 dom方式解析XML原理 XML文件 <?xml version="1.0" encoding="UTF-8 ...

  8. [转]说说C语言运算符的“优先级”与“结合性”

    补充自己的一点理解: 1.关于++i 与 i++的区别. ++i 和 i++如果是单独使用的语句,即二者后面均加上分号,或者其他单独使用的语句,没有任何区别.例如: for(i=0;i<100; ...

  9. D6 I

    I - I Time Limit:1000MS     Memory Limit:2048KB     64bit IO Format:%lld & %llu Submit Status Pr ...

  10. UVa11324 The Largest Clique(强连通分量+缩点+记忆化搜索)

    题目给一张有向图G,要在其传递闭包T(G)上删除若干点,使得留下来的所有点具有单连通性,问最多能留下几个点. 其实这道题在T(G)上的连通性等同于在G上的连通性,所以考虑G就行了. 那么问题就简单了, ...