hdu 1671 Phone List(字典树)题解
题意:给一连串数字,如果有前缀重复给出NO,否则给出YES
思路:这道题要delete否则爆内存,之前想的直接在insert()里解决查询有错误,所以先保存数据再查询。
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define ll long long
const int N=10005;
const int INF=1e9;
using namespace std;
char s[N][15];
struct Trie{
int num;
Trie *next[10];
Trie(){
num=0;
for(int i=0;i<10;i++){
next[i]=NULL;
}
}
};
Trie *root;
void insert(char s[]){
int len=strlen(s);
Trie *p=root;
for(int i=0;i<len;i++){
int v=s[i]-'0';
if(p->next[v]==NULL){
p->next[v]=new Trie();
}
p=p->next[v];
}
if(!p->num){
p->num++;
}
}
int query(char s[]){
int len=strlen(s),v;
Trie *p=root;
for(int i=0;i<len;i++){
v=s[i]-'0';
p=p->next[v];
if(p->num && i!=len-1) return 1;
}
return 0;
}
void del(Trie *p){
if(p==NULL) return;
for(int i=0;i<10;i++){
if(p->next[i]!=NULL) del(p->next[i]);
}
delete p;
}
int main(){
int flag,t,n;
scanf("%d",&t);
while(t--){
root=new Trie();
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%s",s[i]);
insert(s[i]);
}
flag=0;
for(int i=0;i<n;i++){
if(flag) break;
flag=query(s[i]);
}
if(flag) printf("NO\n");
else printf("YES\n");
del(root);
}
return 0;
}
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 5536 Chip Factory 字典树
Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- hdu 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- 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 ...
- HDU 1298 T9【字典树增加||查询】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1298 T9 Time Limit: 2000/1000 MS (Java/Others) Memo ...
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
随机推荐
- 完美解决onchange不能实时的监听
我们大家都知道onchange有时候很不好用,因为onchange事件是离开焦点后才会被触发,而不是实时去监听! 那么oninput()事件和onpropertychange()完美的解决了问题:(o ...
- 【make install】自定义安装目录,添加动态链接库 【--prefix】 【ldconfig】 【LD_LIBRARY_PATH】
怎么卸载make install安装的软件? https://www.zhihu.com/question/20092756 怎么指定安装目录以及对应的添加动态库的方法 linux库在不指定安装路径时 ...
- Elegant Construction---hdu5813(构造图)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5813 题意是:有n个点,每个点都能到达num个点,让我们构造任意一个有向图满足条件,即:使得 i 能到 ...
- SQL Server中灾难时备份结尾日志(Tail of log)的两种方法
转自:http://www.cnblogs.com/CareySon/archive/2012/02/23/2365006.html SQL Server中灾难时备份结尾日志(Tail of log) ...
- VUE 使用之:nextTick
<div class="app"> <div ref="msgDiv">{{msg}}</div> <div v-if ...
- 【深入理解javascript】执行上下文
参考原文:执行上下文 1.每一个执行上下文,工作分为三个阶段: 准备阶段–>执行阶段–>调用阶段 准备阶段:代码执行之前,设置数据,相当于初始化. 执行阶段:开始执行每一行代码. 调用阶段 ...
- latex 转word
1:下载pandoc软件,支持多种文件格式互转. http://www.pandoc.org/installing.html#windows 2:下载zip包,解压,并将含有pandoc.exe的目录 ...
- [dt]世纪历史长河年代表
年代口诀 夏商与西周, 东周分两段, 春秋和战国, 一统秦两汉, 三分魏蜀吴, 二晋前后延, 南北朝并列, 隋唐五代传, 宋元明清后, 皇朝至此完. 中国历史长河年代表 参考: 中国历史朝代顺序表.年 ...
- MySQL架构简介
- 自动生产jason的工具
EnjoySR/ESJsonFormat-Xcode