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. CF578C:Weakness and Poorness——题解

    https://vjudge.net/problem/CodeForces-578C —————————————————————————— 题目大意:序列的数-x,求最大连续子序列和的绝对值的最小值. ...

  2. 洛谷3794:签到题IV——题解

    https://www.luogu.org/problemnew/show/P3794 题目见上. 有一个套路(虽然我到现在还不会),就是固定一个端点,二分查右端点. 显然这题的正解是O(nlogn) ...

  3. 学习tcpIp必备的抓包工具wireshark

    wireshark是一个优秀的抓包工具 ip.src=192.168.10.123  发送http的一端 ip.dst=192.168.10.126 接收http的一端 如下图所示:

  4. printk打印机别

    1.查看当前控制台的打印级别 cat /proc/sys/kernel/printk 4    4    1    7 其中第一个“4”表示内核打印函数printk的打印级别,只有级别比他高的信息才能 ...

  5. 使用tcpdump监控网络消息发送

    tcpdump是一个用于截取网络分组,并输出分组内容的工具,简单说就是数据包抓包工具.tcpdump凭借强大的功能和灵活的截取策略,使其成为Linux系统下用于网络分析和问题排查的首选工具. tcpd ...

  6. Sqlserver中如何创建链接服务器

    链接服务器在跨数据库/跨服务器查询时非常有用(比如分布式数据库系统中),我将以图文方式详细说明如何利用SQL Server Management Studio在图形界面下创建链接服务器 方法/步骤   ...

  7. webservice 针对WebService服务,客户端调用时报序列化的最大项数maxItemsInObjectGraph超过65536问题

    今天在使用webservice服务时候,报异常“The InnerException message was 'Maximum number of items that can be serializ ...

  8. MyBatis注解Annotation介绍及Demo

     MyBatis注解Annotation介绍及Demo 2014-04-21 17:09:55 标签:Mybatis Annotation 注解 ResultMap SqlBuilder 原创作品,允 ...

  9. 【poj2104-求区间第k大数(不修改)】主席树/可持续化线段树

    第一道主席树~然而是道比较水的...因为它不用修改... 转载一个让我看懂的主席树的讲解吧:http://blog.csdn.net/regina8023/article/details/419106 ...

  10. bzoj 2037: [Sdoi2008]Sue的小球——dp

    Description Sue和Sandy最近迷上了一个电脑游戏,这个游戏的故事发在美丽神秘并且充满刺激的大海上,Sue有一支轻便小巧的小船.然而,Sue的目标并不是当一个海盗,而是要收集空中漂浮的彩 ...