HDU-1671
Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12684 Accepted Submission(s): 4307
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.
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.
YES
/**
题意:给出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的更多相关文章
- HDU 1671 Phone List(Trie的应用与内存释放)
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 1671 Phone List 字典树
// hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...
- HDU 1671 (字典树统计是否有前缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...
- POJ 3630 , HDU 1671 Phone List - from lanshui_Yang
这道题也是一道找前缀的问题,很自然地要用到Trie树,但是如果用动态Trie树(即用指针开辟内存)的话,虽然在HDU上可以过(可能是HDU的数据比较水),但在POJ上会TLE , 所以这道题只能用静态 ...
- HDU 1671 Phone List (Trie)
pid=1671">Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 1671 Phone List
一道字典树的模板题,每次插入前查询是否有该串的某个前缀子串存在,或者该串是否某个串的前缀.具体实现是在插入时串的结尾做一个标记,如果某一个串在查询的时候找到一个标记,说明存在前缀:第二种情况是这个串遍 ...
- HDU 1671 Phone List (Trie·数组实现)
链接:http://blog.csdn.net/acvay/article/details/47089657 题意 给你一组电话号码 判断其中是否有某个电话是另一个电话的前缀 字典树的基础应用 ...
- HDU 1671 Phone List(字符处理)
题目 用字典树可以过,可是我写的字典树一直各种错误,,, 所以,我用了别的更简便的方法.. //去你妹的一直有问题的字典树!!! ////字典树,树的根是空的 // ////#include<i ...
- HDU 1671 Phone List(POJ 3630)
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- [ACM] hdu 1671 Phone List (特里)
Phone List Problem Description Given a list of phone numbers, determine if it is consistent in the s ...
随机推荐
- HDOJ(HDU).1412 {A} + {B} (STL SET)
HDOJ(HDU).1412 {A} + {B} (STL SET) 点我挑战题目 题意分析 大水题,会了set直接用set即可. 利用的是set的互异性(同一元素有且仅有一项). #include ...
- 四连测Day2
题目:链接: https://pan.baidu.com/s/1ef_9hGBhczW0B4dz5IUKmw 密码: qgjy T1: hash后直接二分查询即可 #include<iostre ...
- JAVA对象的深度克隆
有时候,我们需要把对象A的所有值复制给对象B(B = A),但是这样用等号给赋值你会发现,当B中的某个对象值改变时,同时也会修改到A中相应对象的值! 也许你会说,用clone()不就行了?!你的想法只 ...
- [nginx]nginx rewrite规则之last和break
c俺靠这篇博文 http://eyesmore.iteye.com/blog/1142162 有用的配置: 1.开启rewrite_log,这样在/var/log/nginx/error.log中显示 ...
- zabbix监控ipmi
@1.假设idrac已经启用,并且使用ipmitool能正确读取数据(本文后面详细描述如何配置) @2.第一节只涉及web配置 1.创建主机 configuration-->hosts--> ...
- 【题解】ZJOI2009 假期的宿舍 网络流 最大流
好久没有来写博客啦,来水一发. 网络流建模首先很容易想到,如果一个人能睡一张床,那么在这个人和这张床之间连接一条容量为1的边从s向每个需要住宿的人连容量为1的边,表示这个人需要住宿从每张床向t连容量为 ...
- POJ2155 树状数组
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 26650 Accepted: 9825 Descripti ...
- Web前端工程师-优秀简历汇总
Web前端工程师-优秀简历汇总 1. http://www.linqing07.com/resume.html 2. http://www.flqin.com/#page2 3. ht ...
- CSS实现三列布局方法总结
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABYwAAAI7CAYAAABPx9+YAAARJElEQVR4nO3cwWnDQBBA0TioJrXhTl
- webstorm常用功能快捷方式
1 自动注释和撤销注释:ctrl+/ 在一句代码前面用 ctrl+/ 可以自动注释和撤销注释,js,html都可以,很好的省去了敲注释符的时间 (mac下为command+/,下同) 2 自动补全ht ...