HDU 1671 Phone List(POJ 3630)
Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18202 Accepted Submission(s): 6115
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.
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std; struct Node{
Node* pt[10];
bool isEnd;
};
Node* root; int n;
string s[10005];
Node memory[100000];
int allo; void trie_init()
{
memset(memory, 0, sizeof(memory));
allo = 0;
root = &memory[allo++];
} bool trie_insert(string& str)
{
Node *p = root, *q;
for(int i = 0; str[i] != '\0'; ++i){
int id = str[i]-'0';
if(p->pt[id] == NULL){
q = &memory[allo++];
p->pt[id] = q;
}
else{
if(p->pt[id]->isEnd)
return false;
}
p = p->pt[id];
}
p->isEnd = true;
return true;
} void solve()
{
trie_init();
bool ok = true;
for(int i = 0; i < n; ++i){
ok = trie_insert(s[i]);
if(!ok){
cout<<"NO"<<endl;
return;
}
}
cout<<"YES"<<endl;
} bool cmp(const string& a, const string& b)
{
return a.length() < b.length();
} int main()
{
int t;
cin>>t;
while(t--){
cin>>n;
for(int i = 0; i < n; ++i)
cin>>s[i];
sort(s, s+n, cmp);
solve();
}
return 0;
}
HDU 1671 Phone List(POJ 3630)的更多相关文章
- 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,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...
- POJ 3630 , HDU 1671 Phone List - from lanshui_Yang
这道题也是一道找前缀的问题,很自然地要用到Trie树,但是如果用动态Trie树(即用指针开辟内存)的话,虽然在HDU上可以过(可能是HDU的数据比较水),但在POJ上会TLE , 所以这道题只能用静态 ...
- hdu 1671&& poj 3630 (trie 树应用)
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25280 Accepted: 7678 Descr ...
- Hdu 3887 Counting Offspring \ Poj 3321 Apple Tree \BZOJ 1103 [POI2007]大都市meg
这几个题练习DFS序的一些应用. 问题引入: 给定一颗n(n <= 10^5)个节点的有根树,每个节点标有权值,现有如下两种操作: 1.C x y 以节点x的权值修改为y. 2.Q x ...
- POJ 3630 Phone List(trie树的简单应用)
题目链接:http://poj.org/problem?id=3630 题意:给你多个字符串,如果其中任意两个字符串满足一个是另一个的前缀,那么输出NO,否则输出YES 思路:简单的trie树应用,插 ...
- poj 3630 Phone List(字典树)
题目链接: http://poj.org/problem?id=3630 思路分析: 求在字符串中是否存在某个字符串为另一字符串的前缀: 即对于某个字符串而言,其是否为某个字符串的前缀,或存在某个其先 ...
- 【POJ 3630】 Phone List
[题目链接] http://poj.org/problem?id=3630 [算法] 字典树 [代码] #include <algorithm> #include <bitset&g ...
- HDU 1671 (字典树统计是否有前缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...
随机推荐
- 手动挂载安装VMware tools
在VMware 10上装了Red Hat Enterprise Linux 4后,点击“安装VMware tools”后,虚拟机桌面一直不出现挂载了VMware tools的虚拟光驱.在/mnt 和/ ...
- 欧拉工程第60题:Prime pair sets
题目链接 五个数,任意两个数的任意链接后的数还是质数 满足这个条件的最小五个数的和是多少? 结果:26033 纯暴力破解: package projecteuler51to60; import jav ...
- Sina App Engine(SAE)入门教程(9)- SaeMail(邮件)使用
参考资料: SAE mail api 文档 怎么使用? 参见代码: <?php $mail = new SaeMail(); $f = new SaeFetchurl(); $img_data ...
- Delphi语言获得生命的原因和过程
都说Anders Hejlsberg是Delphi语言的作者,前一阵仔细读了VCL源码,惊叹于它的巧夺天工,未免对编译器的作者有些不服气,觉得首功不是他.今天仔细想了想,还是觉得不服不行.以下是我的理 ...
- Executing Raw SQL Queries using Entity Framework
原文 Executing Raw SQL Queries using Entity Framework While working with Entity Framework developers m ...
- Python中的两种结构dict和set
Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 假设要根据同学的名字查找对应的成绩 如果 ...
- SELinux开启与关闭
SELinux是「Security-Enhanced Linux」的简称,是美国国家安全局「NSA=The National Security Agency」 和SCC(Secure Computin ...
- JCIFS读取远程服务器文件过慢的解决方法
JCIFS读取远程服务器文件过慢的解决方法 发表于3年前(2013-07-12 11:23) 阅读(1174) | 评论(0) // 我要收藏"; var favor_del = &qu ...
- MapReduce编程系列 — 5:单表关联
1.项目名称: 2.项目数据: chile parentTom LucyTom JackJone LucyJone JackLucy MaryLucy Ben ...
- 转:java两个jre目录和三个lib目录
lib目录下放置着jar包.程序中的import语句找的就是这些文件!例如:import javax.servlet.RequestDispatcher; 问题在于,在cmd模式下编译,系统会提 ...