POJ 3630 , HDU 1671 Phone List - from lanshui_Yang
这道题也是一道找前缀的问题,很自然地要用到Trie树,但是如果用动态Trie树(即用指针开辟内存)的话,虽然在HDU上可以过(可能是HDU的数据比较水),但在POJ上会TLE , 所以这道题只能用静态Trie树。
实现过程如下:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#define mem(a , b) memset(a , b , sizeof(a))
using namespace std ;
const int MAXN = 1e5 + 5 ;
struct Tnode
{
int count ; // 记录该点是否存在单词
int next[15] ;
Tnode()
{
mem(next , 0) ;
count = 0 ;
}
} TT[MAXN] ;
int pan ; // 判断标记
char s[20] ;
int cnt ; // 序号变量
void inse(char *str)
{
int p = 0 ; // 0 为根节点
int id ;
while (*str)
{
id = *str - '0' ;
if(TT[p].next[id] == 0)
{
TT[p].next[id] = ++ cnt ;
}
p = TT[p].next[id] ;
if(TT[p].count > 0) // 此处判断是否有其他字符串 是 当前字符串 str 的前缀
{
pan = 1 ;
}
++ str ;
}
int i ;
for(i = 0 ; i < 15 ; i ++) // 此处判断 当前字符串str 是否是其他字符串的前缀
{
if(TT[p].next[i] > 0)
{
pan = 1 ;
return ;
}
}
if(TT[p].count == 0)
TT[p].count ++ ;
}
void dele() // 清空原来的结点
{
int i ;
for( i = 0 ; i < MAXN ; i ++)
{
mem(TT[i].next , 0) ;
TT[i].count = 0 ; // 注意此处, 千万不要忘记 !!
}
}
int main()
{
int T ;
scanf("%d" , &T) ;
while (T --)
{
int n ;
scanf("%d" , &n) ;
pan = 0 ;
cnt = 0 ;
while (n --)
{
scanf("%s" , s) ;
inse(s) ;
}
if(pan == 1)
puts("NO") ;
else
puts("YES") ;
dele() ; // 清空
}
return 0 ;
}
POJ 3630 , HDU 1671 Phone List - from lanshui_Yang的更多相关文章
- POJ 2104&HDU 2665 Kth number(主席树入门+离散化)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 50247 Accepted: 17101 Ca ...
- HDU 1671 Phone List(Trie的应用与内存释放)
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
- Eight POJ - 1077 HDU - 1043 八数码
Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...
- hdu 1671 Phone List 字典树
// hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...
- POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算
求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...
- HDU 1671 Phone List(POJ 3630)
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 1671&& poj 3630 (trie 树应用)
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25280 Accepted: 7678 Descr ...
- POJ 1325、ZOJ 1364、HDU 1150 Machine Schedule - from lanshui_Yang
Problem Description As we all know, machine scheduling is a very classical problem in computer scien ...
随机推荐
- Python3 如何优雅地使用正则表达式(详解二)
使用正则表达式 现在我们开始来写一些简单的正则表达式吧.Python 通过 re 模块为正则表达式引擎提供一个接口,同时允许你将正则表达式编译成模式对象,并用它们来进行匹配. 小甲鱼解释:re 模块是 ...
- 探讨VMP 2.12.3 导入表修复
壳版本:VMProtect.Ultimate.2.12.3 样本:notepad.exe 目的:IAT修复 作者:MrWrong 标题:探讨VMP 2.12.3 导入表修复 只是感兴趣,没有其他目的. ...
- 转:VmWare下安装CentOS6图文安装教程
文章来自于:http://www.cnblogs.com/seesea125/archive/2012/02/25/2368255.html 查看文章索引请通过http://www.cnblogs.c ...
- ionic ng-src 在网页显示,但是导出apk在android手机中运行不显示图片
解决方法参照: http://stackoverflow.com/questions/29896158/load-image-using-ng-src-in-android-ionic-aplicat ...
- JSP标准库标签 ———C标签
一.C标签 一] <c:out value="..." default="..." escapeXml="true"> ...
- JSON stringify and parse
来源 : http://javascript.ruanyifeng.com/stdlib/date.html //解析json也可以传入一个方法, 基本上和stringify差不多,不过是逆序的, 要 ...
- 单列模式 (singleton pattern)
单列就是说一个类只能被实例化一次,重点是确保某个对象只有一个,不会有第2个. c# 的实现是这样的 代码来源 : http://www.cnblogs.com/zhili/p/3185302.html ...
- MyBatis insert后返回自增字段的值
如下情况适用支持自增的DB,如MySQL.其他情况参见:MyBatis魔法堂:Insert操作详解(返回主键.批量插入) 1.model public class UserInfo { private ...
- HDOJ 1390 Binary Numbers(进制问题)
Problem Description Given a positive integer n, find the positions of all 1's in its binary represen ...
- JS瀑布流效果
本篇内容实现类似百度图片的呈现功能,瀑布流+自动加载 index13.html <!DOCTYPE html> <html> <head> <meta cha ...