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 ...
随机推荐
- 使用QT设计师-信号和槽signal-slot(第一弹)
自定义信号和槽的步骤: 1.定义信号---signal1 = pyqtSignal() 2.定义槽信号---def setSlot(self): 3.连接信号和槽函数---signal1.connec ...
- 通过PyPI镜像安装Python包
有时,我们直接用pip install package_name安装包时,下载很慢,我们可以尝试从PyPI镜像下载包. 这里是镜像列表和目前的状态: http://www.pypi-mirrors.o ...
- Loadrnner 参数化策略
参数化策略 关键:类型+数据+策略 1.Select next row ( 如何取) 选择下一行 1)Sequential:顺序的 每个VU都从第一行开始,顺序依次向下取值:数据可以循环重复使用:-- ...
- SQL Server中灾难时备份结尾日志(Tail of log)的两种方法
转自:http://www.cnblogs.com/CareySon/archive/2012/02/23/2365006.html SQL Server中灾难时备份结尾日志(Tail of log) ...
- 什么是 Delta 文件
什么是 Delta 文件 Delta 文件应用很广泛,特别是在数据库领域 What Is a Delta File? During most computer operations, copying, ...
- literallycanvas的简介
LiterallyCanvas是什么 Literally Canvas是一个可扩展的开源(BSD许可)HTML5绘图组件,可以用于网页中插入画图板,类似于windows自带的画图板.可以用可视化工具绘 ...
- [LeetCode] questions conclustion_Path in Tree
Path in Tree: [LeetCode] 112. Path Sum_Easy tag: DFS input: root, target, return True if exi ...
- type Props={};
Components Learn how to type React class components and stateless functional components with Flow Se ...
- c++中的构造(包括移动),赋值(包括移动),析构详解
这五种操作:构造(包括移动),赋值(包括移动),析构其实就是定义了对一个对象进行构造,赋值,析构时的行为.理解这些行为并不复杂,复杂的是理解在继承下这些行为的表现.需要注意的是他们并不会被继承(传统意 ...
- 4:7 Struts实现Ajax
不使用插件: 返回数据: 使用插件: Action里面直接给User赋值,然后在前台拿值. type="json":表示返回json对象: root:表示作为跟对象 include ...