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. storm trident的filter和函数

    目的:通过kafka输出的信息进行过滤,添加指定的字段后,进行打印 SentenceSpout: package Trident; import java.util.HashMap; import j ...

  2. Ubuntu安装新英伟达驱动出现问题解决方法

    ERROR: The Nouveau kernel driver is currently in use by your system. This driver is incompatible wit ...

  3. SVN几个重要的问题

    本文不是系统地讲解SVN,只是对SVN中一些重要的或者笔者一直混淆的问题做简要归纳. SVN的安装可以参考笔者的另一篇技术随笔<SVN安装使用小结>. 1.既然能够通过SVN得到“每一个版 ...

  4. Ambiguous mapping. Cannot map 'registerController' method

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappi ...

  5. winhex与磁盘格式与 数据恢复

    第一阶段: 熟悉WinHex的使用. n 熟悉磁盘工具的使用. n 利用WinHex查看物理磁盘和逻辑磁盘. n 了解WinHex中相关工具的用法. 以管理员身份运行winhex(以便之后修改) 上方 ...

  6. core 下使用 autofac

    依赖注入小伙伴们比较常了,这里只说core 下autofac依赖注入的使用 ,不多费话,直接代码. 在 Startup.cs里 public void ConfigureServices(IServi ...

  7. CAD交互绘制mcdbsolid对象(网页版)

    主要用到函数说明: _DMxDrawX::DrawSolid 绘McDbSolid对象.详细说明如下: 参数 说明 DOUBLE dX1 第一个点X DOUBLE dY1 第一个点Y DOUBLE d ...

  8. Codeforces Round #277.5 (Div. 2)-D. Unbearable Controversy of Being

    http://codeforces.com/problemset/problem/489/D D. Unbearable Controversy of Being time limit per tes ...

  9. spring中常用的注解

    使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...

  10. tp5对接支付宝支付简单集成

    对于每个刚开始工作的新手来说,无论支付宝支付还是微信支付都是跑不掉的一个小门槛. 在加上本人比较技术比较渣(比较懒导致的),不太喜欢引用那么大的SDK,于是就简单集成了一下支付宝的支付. 但也只是只有 ...