Phone List

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

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
 Source
/**
题意:给出n个电话号码,要求是否存在有的号码是另外一个字符串的前缀
做法:Trie树 一直是RE 是把主函数的root = new node() 写成 p = new node()
然后 C++MLE 然后每次询问完删除树就OK
**/
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
char ch[][];
struct node
{
int count;
node *next[];
node()
{
count = ;
for(int i=;i<;i++)
{
next[i] = NULL;
}
}
};
node *root = new node(),*p;
void insert(char *s)
{
p = root;
for(int i=;i<strlen(s);i++)
{
int tt = s[i] -'';
if(p->next[tt] == NULL) p->next[tt] = new node();
p = p->next[tt];
p->count++;
}
}
int find(char *c)
{
int i;
p = root;
int len = strlen(c);
for(i=;i<len;i++)
{
int tt = c[i]-'';
if(p->next[tt]->count== ) break;
p = p->next[tt];
}
if(i == len) return ;
else return ;
}
void freedom(node *pp)
{
for(int i=;i<;i++)
{
if(pp->next[i] != NULL)
freedom(pp->next[i]);
}
free(pp);
}
int main()
{
// freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
root = new node();
int n;
bool flag = true;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%s",ch[i]);
insert(ch[i]);
}
for(int i=;i<n;i++)
{
if(find(ch[i]) == )
{
flag = false;
break;
}
}
if(!flag) printf("NO\n");
else printf("YES\n");
freedom(root);
}
return ;
}
 

HDU-1671的更多相关文章

  1. HDU 1671 Phone List(Trie的应用与内存释放)

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. hdu 1671 Phone List 字典树

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

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

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

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

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

  5. HDU 1671 Phone List (Trie)

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

  6. HDU 1671 Phone List

    一道字典树的模板题,每次插入前查询是否有该串的某个前缀子串存在,或者该串是否某个串的前缀.具体实现是在插入时串的结尾做一个标记,如果某一个串在查询的时候找到一个标记,说明存在前缀:第二种情况是这个串遍 ...

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

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

  8. HDU 1671 Phone List(字符处理)

    题目 用字典树可以过,可是我写的字典树一直各种错误,,, 所以,我用了别的更简便的方法.. //去你妹的一直有问题的字典树!!! ////字典树,树的根是空的 // ////#include<i ...

  9. HDU 1671 Phone List(POJ 3630)

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  10. [ACM] hdu 1671 Phone List (特里)

    Phone List Problem Description Given a list of phone numbers, determine if it is consistent in the s ...

随机推荐

  1. Static全局变量与普通的全局变量有什么区别?static函数与普通函数有什么区别?

    Static全局变量与普通的全局变量有什么区别? 答: 全局变量(外部变量)的说明之前再冠以static就构成了静态的全局变量.全局变量本身就是静态存储方式,静态全局变量当然也是静态存储方式. 这两者 ...

  2. HDOJ(HDU).1045 Fire Net (DFS)

    HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...

  3. JavaScript正则表达式的浏览器的差异

    JavaScript中的正则表达式在不同的浏览器中得到的结果可能会有差异,下面把正则表达式在五大主流浏览器(IE.Firefox.Chrome.Safari.Opera,以当前版本为准)之间的差异整理 ...

  4. 修改tomcat编码格式 & tomcat发布WEB项目供外网访问

    1.修改tomcat默认编码格式: 修改tomcat下的conf/server.xml文件,找到如下代码:       <Connector port="8080" prot ...

  5. ACE反应器(Reactor)模式(2)

    转载于:http://www.cnblogs.com/TianFang/archive/2006/12/18/595808.html 在Socket编程中,常见的事件就是"读就绪" ...

  6. mybatis主键返回的实现

    向数据库中插入数据时,大多数情况都会使用自增列或者UUID做为主键.主键的值都是插入之前无法知道的,但很多情况下我们在插入数据后需要使用刚刚插入数据的主键,比如向两张关联表A.B中插入数据(A的主键是 ...

  7. zookeeper的maxSessionTimeout默认值导致hbase regionserver超时

    zookeeper的maxSessionTimeout默认值导致hbase regionserver超时 在hbase中经常会遇到regionserver挂掉的情况,查看日志会看到这样的错误信息 20 ...

  8. c# MD5盐值加密

    using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptograph ...

  9. hive获取日期对应的星期

    pmod(datediff(order_date,'2000-01-02'),7)

  10. [洛谷P1338] 末日的传说

    洛谷题目链接:末日的传说 题目描述 只要是参加jsoi活动的同学一定都听说过Hanoi塔的传说:三根柱子上的金片每天被移动一次,当所有的金片都被移完之后,世界末日也就随之降临了. 在古老东方的幻想乡, ...