【字典树】HDU1671Phone List(论释放内存的重要性)
2015-11-08 14:32
288 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671
一道简单的模板题,但是这个题目是多组数据。所以如果你不释放内存,很有可能你就会MLE到死。。。orz....
增加一个释放内存的函数就可以轻松搞定了。不过最好还是能够养成这样一个习惯,那就是在使用数据结构的指针是,每次都能够释放。
一道简单的模板题,但是这个题目是多组数据。所以如果你不释放内存,很有可能你就会MLE到死。。。orz....
增加一个释放内存的函数就可以轻松搞定了。不过最好还是能够养成这样一个习惯,那就是在使用数据结构的指针是,每次都能够释放。
#include<iostream> #include<string> #include<cstdio> #include<cstring> #include<stdlib.h> using namespace std; const int N=10; char s[10002][11]; struct node{ bool flag; node *next ; node(){ flag=false; memset(next,0,sizeof(next)); } }; node *p,*head=new node(); void Insert(char s[]) { p=head; int i=0; while(s[i]){ int id=s[i++]-'0'; if(p->next[id]==NULL) p->next[id]=new node(); p=p->next[id]; } p->flag=true; } bool Query(char s[]) { p=head; int i=0; while(s[i]){ int id=s[i++]-'0'; if(p->next[id]==NULL) return false; if(p->flag) return true; p=p->next[id]; } return false; } int deal(node* T) // 释放内存空间,尤为重要。 { int i; for(i=0;i<=9;i++) { if(T->next[i]!=NULL) deal(T->next[i]); } free(T); // 头文件 "stdlib.h" return 0; } int main() { int t,n,i; cin>>t; while(t--){ cin>>n; head=new node(); for(i=0;i<n;i++){ cin>>s[i]; Insert(s[i]); } for(i=0;i<n;i++){ if(Query(s[i])){ printf("NO\n"); break; } } if(i==n) printf("YES\n"); deal(head); } return 0; }
相关文章推荐
- 字典树的基本知识及使用C语言的相关实现
- iframe如何动态创建及释放其所占内存
- Shell实现强制释放内存脚本分享
- 解析PHP中的unset究竟会不会释放内存
- 解析PHP中的内存管理,PHP动态分配和释放内存
- Trie树_字典树(字符串排序)简介及实现
- Linux系统内存不够用怎么办?释放Linux内存的教程
- 深入理解PHP内存管理之谁动了我的内存
- POJ 1056 IMMEDIATE DECODABILITY
- Problem A: ZZ买衣服
- codeforces content#260 D A Lot of Games
- sdutoj1500 Message Flood
- 字典树
- Hardwood Species——物种所占比例
- Message Flood——发短信
- HDU 1671 字典树
- hdoj 4287 Intelligent IME 字典树
- 字典树(Trie树、单词查找树、前缀树)
- Immediate Decodability
- 统计难题