题意:给一连串数字,如果有前缀重复给出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(字典树)题解的更多相关文章

  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 5536 Chip Factory 字典树

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

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

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

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

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

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

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

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

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

  9. HDU 1298 T9【字典树增加||查询】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1298 T9 Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  10. hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)

    统计难题Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submis ...

随机推荐

  1. sql两列相除,保留n位小数

    ), ) from tablename 以上代码意思两列相处,然后保留4位小数.

  2. 虫师的性能测试思想html网页学习

    http://www.cnblogs.com/fnng/category/387349.html

  3. list的方法、操作

    序号 分类 关键字 / 函数 / 方法 说明 1 增加 列表.insert(索引, 数据) 在指定位置插入数据     列表.append(数据) 在末尾追加数据     列表.extend(列表2) ...

  4. NYOJ 587 blockhouses 【DFS】

    blockhouses 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 Suppose that we have a square city with straigh ...

  5. vue-页面回退

    <template> <div> <button @click="goback">我是Home01</button> </di ...

  6. docker学习(3)--Dockfile详解

    转载请注明出处:http://www.cnblogs.com/lighten/p/6900556.html 1.基本说明 Dockfile是一个用于编写docker镜像生成过程的文件,其有特定的语法. ...

  7. U盘安装win10操作系统

    https://www.zhihu.com/question/39207359   1:进入微软官方网站,点击立即下载工具,下载完成mediacreationtool,双击打开,接受协议  https ...

  8. (转)Elasticsearch查询规则------match和term

    es种有两种查询模式,一种是像传递URL参数一样去传递查询语句,被称为简单搜索或查询字符串(query string)搜索,比如 GET /megacorp/employee/_search //查询 ...

  9. (转)Elasticsearch NoNodeAvailableException None of the configured nodes are available

    问题背景:将es部署到内网中两台服务器,其Ip地址分别为:192.111.222.5,192.111.222.1(部署方式完全一样,是将192.111.222.1服务器上es整个部署包,拷贝到了192 ...

  10. Django之分页功能

    Django提供了一个新的类来帮助你管理分页数据,这个类存放在django/core/paginator.py.它可以接收列表.元组或其它可迭代的对象. 基本语法 class Paginator(ob ...