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. php 用封装类的方法操作数据库和批量删除

    封装类 <?php class DBDA { public $host="localhost"; //服务器地址 public $uid="root"; ...

  2. 发现磁盘的shell

    #!/bin/bash #set -x diskarray=(`cat /proc/diskstats |grep -E "\bsd[a-z]\b|\bxvd[a-z]\b|\bvd[a-z ...

  3. Web性能测试中的几个关键指标

    系统吞吐量 吞吐量指单位时间内系统处理的请求数量,体现系统的整体处理能力.对于网站,可以用"请求数/秒"或是"页面数/秒"来衡量,也可以用"访问人数/ ...

  4. 学SEO你其实只需要半个钟

    网站上线之前: 关键词的分析以及选择: 关键词在我们网站的每个页面:首页,栏目页,文章都存在,它定位了你的网站的这个页面是做什么的,有什么内容,也是SEO中的最重要的部分. 网站必须确定并且设置好关键 ...

  5. phpstorm 8 license key

    Learn Programming===== LICENSE BEGIN =====63758-1204201000000Ryqh0NCC73lpRm!XVcxFChJ2gTUR2lZtlLXrPLb ...

  6. ServletContext中常用方法(getRsource和getResourceAsStream)

    转自:http://blog.csdn.net/yakson/article/details/9203267 一..获取Tomcat的Context的初始化参数. 1.获取Tomcat的server. ...

  7. uva 11380(最大流+拆点)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36707 思路:根据题意拆点建图即可. #include<io ...

  8. Queuing

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission ...

  9. 状压DP POJ 3254 Corn Fields

    题目传送门 /* 状态压缩DP:先处理硬性条件即不能种植的,然后处理左右不相邻的, 接着就是相邻两行查询所有可行的种数并累加 写错一个地方差错N久:) 详细解释:http://www.tuicool. ...

  10. BZOJ3784 : 树上的路径

    树的点分治,在分治的时候将所有点到根的距离依次放入一个数组q中. 对于一棵子树里的点,合法的路径一定是q[L]..q[R]的某个数加上自己到重心的距离. 定义五元组(v,l,m,r,w),表示当前路径 ...