POJ3630/HDU-1671 Phone List,字典树静态建树!
Phone List
POJ动态建树TLE了~~~
题意:拨打某个电话时可能会因为和其他电话号码的前几位重复而导致错误,现在给出一张电话单,求是否有某个电话是其他电话的前缀。是则输出NO,否则输出YES。
思路:字典树模板题了,但有一个动态建树每次都要清空内存这个很重要,很容易导致MLE了。这个题插入和查找可以放在一起,这便是字典树的强大之处。
struct Tree
{
bool f;
Tree *next[N];
};
int insert(Tree *root,char *s)
{
int sign=0;
Tree *p=root;
while(*s!='\0')
{
if(p->next[*s-'0']==NULL)
{
Tree *temp=new Tree;
for(int i=0; i<N; i++) temp->next[i]=NULL;
temp->f=false;
p->next[*s-'0']=temp;
}
if(p->f) sign=1;
p=p->next[*s-'0'];
s++;
}
p->f=true;//构成了一个单词或者电话
for(int i=0; i<N&&!sign; i++) if(p->next[i]!=NULL) sign=1;//这条路径还可以走下去,说明前面的都重复了
return sign;
}
void del(Tree *root)
{
for(int i=0; i<N; i++)
if(root->next[i]!=NULL)
del(root->next[i]);
delete(root);
}
int main()
{
int t,n;
char s[15];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int f=0;
Tree *root=new Tree;
for(int i=0;i<N;i++) root->next[i]=NULL;//初始化子节点;
root->f=false;
for(int i=0; i<n; i++)
{
scanf("%s",s);
if(!f) f=insert(root,s);
}
if(f) printf("NO\n");
else printf("YES\n");
del(root);//每次都要清空内存;
}
return 0;
} /*****************************************
***************** LYQ ***************
***************** YES ***************
*UserID: secrecy *
*RunOJ: *
*RunID: *
*Submit time: *
*Language: G++ *
*Result: Accepted *
*time: *
*Memory: *
*Length: *
*School: NYIST *
*Blog: http://blog.csdn.net/nyist_tc_lyq *
*QQ: *
*Tel: *
*****************************************/
一模一样的题,杭电上用动态建树过了,这里就超时了,建树反复分配和释放内存的问题,所以可以用静态树进行优化一下,开一个Tree的数组,注意开大一点,太大或太小都会MLE。数组开60010差不多可以了。
int cnt;
struct Tree
{
bool f;
Tree *next[N];
};
Tree memory[60005];
int insert(Tree *root,char *s)
{
int sign=0;
Tree *p=root;
while(*s!='\0')
{
if(p->next[*s-'0']==NULL)
p->next[*s-'0']=&memory[cnt++];
if(p->f) sign=1;
p=p->next[*s-'0'];
s++;
}
p->f=true;
for(int i=0; i<N&&!sign; i++) if(p->next[i]!=NULL) sign=1;
return sign;
}
int main()
{
int t,n;
char s[15];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int f=0;
cnt=0;
memset(memory,0,sizeof(memory));//清空
Tree *root=&memory[cnt++];//已经开辟好了内存,不用每次都分配了。
for(int i=0; i<n; i++)
{
scanf("%s",s);
if(!f) f=insert(root,s);
}
if(f) printf("NO\n");
else printf("YES\n");
}
return 0;
}
//能理解但是不会用,关于内存分配和释放这一块比较薄弱,貌似平时用的也不多~~ /*****************************************
***************** LYQ ***************
***************** YES ***************
*UserID: secrecy *
*RunOJ: *
*RunID: *
*Submit time: *
*Language: G++ *
*Result: Accepted *
*time: *
*Memory: *
*Length: *
*School: NYIST *
*Blog: http://blog.csdn.net/nyist_tc_lyq *
*QQ: *
*Tel: *
*****************************************/
POJ3630/HDU-1671 Phone List,字典树静态建树!的更多相关文章
- hdu 1671 Phone List 字典树
// hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...
- [ACM] hdu 1671 Phone List (字典树)
Phone List Problem Description Given a list of phone numbers, determine if it is consistent in the s ...
- hdu 1671 Phone List 字典树模板
Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...
- HDU 2846 Repository (字典树 后缀建树)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- Trie字典树 静态内存
静态字典树 看了好久的字典树,挺简单的一个结构,愣是看了这么久才写出来... 专心一点就不会这样了.... 接下来就去刷刷字典树的题吧....... 下面是字典树.... 定义节点 typedef s ...
- HDU 5536 Chip Factory 字典树
Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- HDU 1298 T9(字典树+dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...
- HDU 2846 Repository(字典树,每个子串建树,*s的使用)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
随机推荐
- 使用Spring Cloud Feign
使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务 在spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就 ...
- Windows下Apache+PHP+MySQL开发环境的搭建(WAMP)
准备工作: 1.下载apache服务器安装包,官网http://www.apache.org/,下载地址:http://httpd.apache.org/download.cgi 2.下载MySQL, ...
- hybrid app开发中:苹果移动设备实用Meta标签
hybrid app开发中:苹果移动设备实用Meta标签 “apple-mobile-web-app-status-bar-style”作用是控制状态栏显示样式 具体效果如下: status-bar- ...
- 用户登陆界面(jquery)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- ef导航属性
https://msdn.microsoft.com/en-us/data/jj574232.aspx 场景是 A表中有B,B表中又C.都是一堆多的关系.怎样Mapping是个问题啊. var ...
- ZOJ 3537 Cake (区间DP,三角形剖分)
题意: 给出平面直角坐标系上的n个点的坐标,表示一个多边形蛋糕,先判断是否是凸多边形,若否,输出"I can't cut.".若是,则对这个蛋糕进行3角形剖分,切n-3次变成n-2 ...
- Java学习之初识线程
“身之主宰便是心,心之所发便是意,意之本体便是知,意之所在便是物 --摘自阳明先生语录” 1.概念 在说线程之前我们先了解关于进程的一些知识,什么是进程? 程序一旦运行就是一个独立的进程,以windo ...
- A. Pride (emmmm练习特判的好题)
题目连接 : http://codeforces.com/problemset/problem/891/A You have an array a with length n, you can per ...
- Oracle数据库同步方案
Oracle数据库同步方案 1. 利用数据泵导出每表前2000行数据 expdp tvpay2/tvpay directory=dmp dumpfile=20170508.dmp include=ta ...
- assign, retain, copy, weak, strong
一.assign, retain, copy 的区别(引用计数 RC reference count) 参考:IOS基础:retain,copy,assign及autorelease 1. 假设你用m ...