题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671

Problem Description
Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let’s say the phone catalogue listed these numbers:

1. Emergency 911

2. Alice 97 625 999

3. Bob 91 12 54 26

In this case, it’s not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob’s phone number. So this list would not be consistent.
 
Input
The first line of input gives a single integer, 1 <= t <= 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 <= n <= 10000. Then follows n lines with one unique phone number on each line. A phone number
is a sequence of at most ten digits.
 
Output
For each test case, output “YES” if the list is consistent, or “NO” otherwise.
 
Sample Input
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
 
Sample Output
NO
YES
 
Source

题意:

统计是否有同样的前缀!

代码例如以下:

#include <cstdio>
#include <cstring>
#include <malloc.h>
#include <iostream>
using namespace std;
#define MAXN 10
typedef struct Trie
{
int v;//依据须要变化
Trie *next[MAXN];
//next是表示每层有多少种类的数,假设仅仅是小写字母,则26就可以,
//若改为大写和小写字母,则是52,若再加上数字,则是62了
}Trie;
Trie* root; void createTrie(char *str)
{
int len = strlen(str);
Trie *p = root, *q;
for(int i = 0; i < len; i++)
{
int id = str[i]-'0';
if(p->next[id] == NULL)
{
q = (Trie *)malloc(sizeof(Trie));
q->v = 1;//初始v==1
for(int j = 0; j < MAXN; j++)
q->next[j] = NULL;
p->next[id] = q;
p = p->next[id];
}
else
{
// p->next[id]->v++;
p = p->next[id];
}
}
p->v = -1;//若为结尾,则将v改成-1表示
} int findTrie(char *str)
{
int len = strlen(str);
Trie *p = root;
for(int i = 0; i < len; i++)
{
int id = str[i]-'0';
p = p->next[id];
if(p == NULL) //若为空集,表示不存以此为前缀的串
return 0;
if(p->v == -1) //字符集中已有串是此串的前缀
return -1;
}
//return p->v;
return -1; //此串是字符集中某串的前缀
}
int dealTrie(Trie* T)
{
//动态字典树,有时会超内存,这是就要记得释放空间了
if(T==NULL)
return 0;
for(int i = 0; i < MAXN; i++)
{
if(T->next[i]!=NULL)
dealTrie(T->next[i]);
}
free(T);
return 0;
}
int main()
{
int t, n;
char str[15];
scanf("%d",&t);
while(t--)
{
int flag = 0;
root = (Trie *)malloc(sizeof(Trie));
for(int i = 0; i < MAXN; i++)
root->next[i] = NULL;
scanf("%d",&n);
for(int i = 0; i < n; i++)
{
scanf("%s",str);
if(findTrie(str) == -1)
{
flag = 1;
continue;
}
createTrie(str);
}
if(flag)
printf("NO\n");
else
printf("YES\n");
dealTrie(root);
}
return 0;
}

HDU 1671 (字典树统计是否有前缀)的更多相关文章

  1. hdu 1671(字典树判断前缀)

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. Phone List HDU - 1671 字典树

    题意:给出一堆一组一组的数字  判断有没有哪一个是另外一个的前缀 思路:字典树 插入的同时进行判断  不过 当处理一组数字的时候 需要考虑的有两点1.是否包含了其他的序列2.是否被其他序列包含 刚开始 ...

  3. HDU 5687 字典树入门

    Problem C Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  4. hdu 2072(字典树模板,set,map均可做)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...

  5. HDU - 1251 字典树模板题

    Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).  Input输入数据的第一部 ...

  6. HDU 5687 字典树插入查找删除

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...

  7. HDU 5384 字典树、AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...

  8. hdu 1247 (字典树入门)

    Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  9. hdu 2112(字典树+最短路)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. Redis学习笔记--String(四)

    Redis的第一个数据类型string 1.命令 1.1赋值 语法:SET key value Set key value; > OK 1.2取值 语法:GET key > get tes ...

  2. 2、qq物联环境搭建

    1.使用easyopenjtag.openjtag来烧写uboot 2.体验uboot 插讲<网络设置_ping问题解决_远程登录ssh_文件互传ftp> 192.168.1.183 wi ...

  3. Instruction-Set Support for Invocation of VMM-Configured Services without VMM Intervention

    A processing core comprising instruction execution logic circuitry and register space. The register ...

  4. android请求

    //请求 HttpURLConnection conn = (HttpURLConnection)new URL(path).openConnection(); conn.setConnecTimeo ...

  5. swift开发网络篇 - post 请求

    /** 所有网络请求,统一使用异步请求! 在今后的开发中,如果使用简单的get/head请求,可以用NSURLConnction异步方法 GET查/POST增/PUT改/DELETE删/HEAD GE ...

  6. Linux下交叉编译gdb,gdbserver+gdb的使用以及通过gdb调试core文件

    交叉编译gdb和gdbserver 1.下载gdb:下载地址为:http://ftp.gnu.org/gnu/gdb/按照一般的想法,最新版本越好,因此下载7.2这个版本.当然,凡事无绝对.我们以gd ...

  7. DSO Framer _ WinForm 使用

    根据自己对dsoframer控件的学习,想把dsoframer控件进行简单的包装为C#的usercontrol,大体需要作如下:(创建windows的usercontrol的步骤就不再说了...)我们 ...

  8. HDU 1408 盐水的故事 数学水题

    http://acm.hdu.edu.cn/showproblem.php?pid=1408 题目: 挂盐水的时候,如果滴起来有规律,先是滴一滴,停一下:然后滴二滴,停一下:再滴三滴,停一下...,现 ...

  9. 致ITFriend用户

    ) 全权处理,相关问题请和他沟通. 祝大家中秋节快乐,一家团团圆圆. 小雷FansUnion   湖北 武汉   2014年9月7日 --------------------------------- ...

  10. oracle数据库的备份与恢复

    一.备份 方法1: PLSQL中进行导出    对于方式1: 对于导出可执行文件的选择,可通过下面的几个参考位置去查找: 导入imp:F:\app\Administrator\product\11.1 ...