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: *
*****************************************/

POJ-3630

一模一样的题,杭电上用动态建树过了,这里就超时了,建树反复分配和释放内存的问题,所以可以用静态树进行优化一下,开一个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: *
*****************************************/

最新文章

  1. chrome新版安装flash控件失败解决方法
  2. gulp入门教程(详细注解)
  3. C++ list size()所想到的事情
  4. BZOJ 1034 泡泡堂
  5. android 上传文件用php程序在服务端接受(一)
  6. python常用内置函数
  7. centos7 开机启动某些程序的方法
  8. hadoop笔记之MapReduce的应用案例(WordCount单词计数)
  9. asp.net webapi 多文件上传
  10. 1129: 零起点学算法36——3n+1问题
  11. ecshop开发帮助
  12. Python基础__字符串拼接、格式化输出与复制
  13. CXF 开发 REST 服务
  14. RAC Wait Event: gcs log flush sync 等待事件 转
  15. 建议3---理解Python与C语言的不同之处
  16. Ubuntu16.04中搭建TFTP 和 NFS 服务器
  17. c# 检查目录,当指定目录不存在时建立目录
  18. spark-submit 提交Application
  19. postman app支持浏览器上的cookie
  20. Intellij IDEA如何使用Maven Tomcat Plugin运行web项目

热门文章

  1. Java 实现Excel表数据的读取和写入 以及过程中可能遇到的问题
  2. IE下的圆角
  3. Permutations(copy)
  4. Google Colab的一些注意事项
  5. WINDOWS-API:API函数大全
  6. 用border实现三角形的过程
  7. tp5对接支付宝支付简单集成
  8. 使用JAVA抓取网页数据
  9. python中with用法及原理
  10. 离线功能对比:service worker和applicationCache