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

POJ3630/HDU-1671 Phone List,字典树静态建树!的更多相关文章

  1. hdu 1671 Phone List 字典树

    // hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...

  2. [ACM] hdu 1671 Phone List (字典树)

    Phone List Problem Description Given a list of phone numbers, determine if it is consistent in the s ...

  3. hdu 1671 Phone List 字典树模板

    Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...

  4. HDU 2846 Repository (字典树 后缀建树)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  5. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  6. Trie字典树 静态内存

    静态字典树 看了好久的字典树,挺简单的一个结构,愣是看了这么久才写出来... 专心一点就不会这样了.... 接下来就去刷刷字典树的题吧....... 下面是字典树.... 定义节点 typedef s ...

  7. HDU 5536 Chip Factory 字典树

    Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  8. HDU 1298 T9(字典树+dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...

  9. HDU 2846 Repository(字典树,每个子串建树,*s的使用)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

随机推荐

  1. Mysql order by 多字段排序

    mysql单个字段降序排序: select * from table order by id desc; mysql单个字段升序排序: select * from table order by id ...

  2. java冒泡排序和快速排序代码

    冒泡排序: package nicetime.com; //基本思想:在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,// 让较大的数往下沉,较小的往上 ...

  3. codevs 3129 奶牛代理商IX

    时间限制: 1 s  空间限制: 32000 KB  题目等级 : 白银 Silver 题目描述 Description 小X从美国回来后,成为了USACO中国区的奶牛销售代理商,专门出售质优价廉的“ ...

  4. Lesson2

    #ifdef __cplusplus #include <cstdlib> #else #include <stdlib.h> #endif #include <SDL/ ...

  5. java读取clob字段的几种方法(转)

    http://blog.csdn.net/tanksyg/article/details/49927897 第一种 Clob clob = rs.getClob("remark") ...

  6. 用valgrind检查内存问题

    Valgrind Valgrind作为一个免费且优秀的工具包,平时大部分人可能都是使用valgrind检测内存问题,如内存泄露,越界等. Valgrind工具包包含多个工具,如Memcheck,Cac ...

  7. Mysql闪回工具之binlog2sql的原理及其使用

    生产上误删数据.误改数据的现象也是时常发生的现象,作为运维这时候就需要出来补锅了,最开始的做法是恢复备份,然后从中找到需要的数据再进行修复,但是这个时间太长了,对于大表少数数据的修复来讲,动作太大,成 ...

  8. python:lambda、filter、map、reduce

    lambda 为关键字.filter,map,reduce为内置函数. lambda:实现python中单行最小函数. g = lambda x: x * 2 #相当于 def g(x): retur ...

  9. 使用bat脚本调用py文件直接获取应用的包名和targetversion

    背景: 在上一篇已经介绍过如何利用python调用aapt获取包名 https://www.cnblogs.com/reseelei-despair/p/11078750.html 但是因为每次都要修 ...

  10. fckeditor的实例

                            第一步:去官网下载,删除多余的包 删除所有”_”开头的文件和文件夹   删除FCKeditor的目录下:   fckeditor.afp fckedit ...