Phone List

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

解题思路:

推断输入的串中是否存在某个串是另外串的前缀。

建立字典树,关键是设立某个串结尾的标志,即在哪个字母结尾。

要推断是否存在前缀要考虑两种情况。一是当前输入的串是否是曾经输入的串的前缀,而是曾经输入的串是否是当前输入的串的前缀。前一种情况在查找该串时,会从第一个字母查找到最后一个字母,中间不返回不论什么值,说明当前的串是曾经的前缀,后一种情况。当在查找当前串的时候遇到曾经串结束的标志。则说明曾经串是当前串的前缀。

代码中结束的标志为保存串中最后一个字母的那个节点的cnt值为-1.

如图:

代码:

#include <iostream>
#include <malloc.h>
#include <algorithm>
#include <string.h>
#include <stdio.h>
using namespace std;
const int maxn=10;
bool ok;
char str[12];
int t,n; struct Trie
{
int cnt;
Trie *next[maxn];
}; 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->cnt = 1;
for(int j=0; j<maxn; ++j)
q->next[j] = NULL;
p->next[id] = q;
p = p->next[id];
}
else
{
p = p->next[id];
}
}
p->cnt=-1;//串末尾的标志
} int findTrie(char *str)
{
int len=strlen(str);
Trie *p=root;
for(int i=0;i<len;i++)
{
int id=str[i]-'0';
if(p->next[id]==NULL)
return 0;//没有建过树
if(p->next[id]->cnt==-1)
return -1;//曾经串是当前串的前缀
p=p->next[id];
}
return -1;//当前串是曾经串的前缀
} void release(Trie *root)//释放空间
{
for(int i=0;i<maxn;i++)
{
if(root->next[i])
release(root->next[i]);
}
free(root);
}
int main()
{
scanf("%d",&t);
while(t--)
{
ok=1;
root=(Trie*)malloc(sizeof(Trie));//root为指针类型,须要申请空间
for(int i=0; i<10; ++i)
root->next[i] = NULL;
scanf("%d",&n);
while(n--)
{
scanf("%s",str);
if(findTrie(str)==-1)
ok=0;//有前缀
if(!ok)
continue;//有前缀,后面的就不用建树了
CreateTrie(str);
}
if(ok)
printf("YES\n");
else
printf("NO\n");
release(root);
}
return 0;
}

[ACM] hdu 1671 Phone List (字典树)的更多相关文章

  1. hdu 1671 Phone List 字典树

    // hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...

  2. hdu 1671 Phone List 字典树模板

    Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...

  3. ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)

    Description We all use cell phone today. And we must be familiar with the intelligent English input ...

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

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

  5. HDU 5536 Chip Factory 字典树

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

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

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

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

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

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

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

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

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

随机推荐

  1. Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls

    Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...

  2. C语言每日小练(四)——勇者斗恶龙

    勇者斗恶龙 你的王国里有一条n个头的恶龙,你希望雇佣一些骑士把它杀死(砍掉全部的头). 村里有m个骑士能够雇佣.一个能力值为x的骑士能够砍掉恶龙一个致敬不超过x的头,且须要支付x个金币. 怎样雇佣骑士 ...

  3. WIN8.1 IE11 F12开发者人员工具,开启什么都不显示

    WIN8.1 IE11 F12开发者人员工具,开启什么都不显示,求大神帮忙?  IE11也没法重装, 还原IE设置过了. 没有效果.   也试过把IE停掉,重启后再重新打开 也没用 已经解决了  删除 ...

  4. 使用Enum.TryParse()实现枚举的安全转换

    在项目中,有时候会用到领域枚举和DTO枚举的映射和转换.有一个现实的问题是:如果领域枚举项发生变化,而DTO枚举项没有及时更新,这样会造成映射不上的问题.那么,如何避免此类问题呢? 先看领域枚举和DT ...

  5. 使用EF Model First创建edmx模型,数据库有数据的情况下,如何同时更新模型和数据库

    使用"EF Model First",我们可以很直观地在edmx文件中创建数据模型,并根据模型生成数据库,整个过程快速而高效.可当数据库中有了一定的数据量,同时,可能需要对模型中字 ...

  6. android 最新 NDK r8 在window下开发环境搭建 安装配置与使用 详细图文讲解,完整实际配置过程记录(原创)

      android 最新 NDK r8 在window下开发环境搭建 安装配置与使用 详细图文讲解,完整实际配置过程记录(原创) 一直想搞NDK开发却一直给其他事情耽搁了,参考了些网上的资料今天终于把 ...

  7. 安全:Web 安全学习笔记

    背景 说来惭愧,6 年的 web 编程生涯,一直没有真正系统的学习 web 安全知识(认证和授权除外),这个月看了一本<Web 安全设计之道>,书中的内容多是从微软官方文档翻译而来,这本书 ...

  8. java操作mongodb时,对象bean和DBObject相互转换的方法

    package com.iqbon.spider.util; import java.lang.reflect.Field; import java.lang.reflect.InvocationTa ...

  9. D - I Think I Need a Houseboat(1.3.1)

    Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Descr ...

  10. 启明星Exchange/outlook预定会议室终端显示解决方案

    启明星会议室预定系统(Exchange2007及其以上版本,)终端调用说明 (一)技术原理 系统采用三级刷新方式,以尽可能减少对服务器的访问压力. (1) exe程序,每隔5分钟访问Exchange, ...