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 ...
随机推荐
- ***解决PHP输出多余的空格或换行
用CI框架写APP后台接口的时候,返回的JSON前面有多余的2哥换行,首先排查的是BOM,结果问题依旧 再就是排查<?php ?> 标签外没有多余的回车.换行,结果发现确实有多余的换行,去 ...
- Robot framework+python安装使用图解版
一.安装包 1.Python2.7(一切的基础,切记安装目录不能有中文不能有空格) 1)python2.7:(python环境):python-2.7.msi 2)setuptools(python包 ...
- CTSC2016&&APIO2016游记
4.30 下午衡中放假,我们因为比赛的缘故提前到中午12:00放假 然后我爸爸说要来接我,直到下午两点多他才到,然后衡中宿舍的楼管阿姨死活不给我开门 莫名其妙的等到了三点多快四点的时候我才跟实验班的一 ...
- hdu 1978 How many ways
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int ...
- Java 按字节获得字符串(中文)长度
引自:http://songjianyong.iteye.com/blog/1552973 package cn.com.songjy.test; import java.io.Unsupported ...
- SRAM与SDRAM的区别
http://www.cnblogs.com/spartan/archive/2011/05/06/2038747.html SDRAM SDRAM(Synchronous Dynamic Rando ...
- C语言文件操作函数
C语言文件操作函数大全 clearerr(清除文件流的错误旗标) 相关函数 feof表头文件 #include<stdio.h> 定义函数 void clearerr(FILE * str ...
- JavaScript 节点操作Dom属性和方法(转)
JavaScript 节点操作Dom属性和方法 一些常用的dom属性和方法,列出来作为手册用. 属性: 1.Attributes 存储节点的属性列表(只读) 2.childNodes 存储 ...
- java知识积累——单元测试和JUnit(二)
首先来复习一下几个重要知识点,然后接着进行一些介绍.在上一篇文章中,我曾经贴过下面这张图片: 在Which method stubs would you like to create?这里,现在结合4 ...
- 查看mssql死锁的详细信息(存储过程)
CREATE procedure [dbo].[sp_who_lock]asbegindeclare @spid int,@bl int, @intTransactionCountOn ...