[ACM] hdu 1671 Phone List (字典树)

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
2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)

解题思路:

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

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

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

代码中结束的标志为保存串中最后一个字母的那个节点的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");elseprintf("NO\n");release(root);}return 0;
}

posted on 2017-04-22 17:30 mthoutai 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/mthoutai/p/6748444.html

[ACM] hdu 1671 Phone List (字典树)相关推荐

  1. hdu 1671 Phone List 字典树模板

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

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

    1 /******************************************************* 2 题目: 统计难题 (hdu 1251) 3 链接: http://acm.hd ...

  3. HDU 1251 统计难题 字典树/STL

    统计难题 Time Limit:2000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Description Ig ...

  4. HDU - 1251 统计难题(字典树)

    题目链接:点击查看 题目大意:给出一些单词,后续再给出一些前缀,询问包含此前缀的单词一共有多少个 题目分析:这个题目的数据可能有点水,而且时间给的也很足,给了两秒,而且加上是hdu的,可以用无序map ...

  5. HDU 5536 Chip Factory 字典树+贪心

    给你n个数,a1....an,求(ai+aj)^ak最大的值,i不等于j不等于k 思路:先建字典树,暴力i,j每次删除他们,然后贪心找k,再恢复i,j,每次和答案取较大的,就是答案,有关异或的貌似很多 ...

  6. HDU - 4825 Xor Sum(字典树)

    题目链接:点击查看 题目大意:给出n个数组成的数组,再给出m个询问,每次询问给出一个x,要求从数组中选出一个k,使得k^x的值最大 题目分析:字典树求异或值最大的模板题,对于n个数直接insert到字 ...

  7. hdu 5384 Danganronpa(字典树)

    题意: f(A,B)表示:B在A中作为子串出现的次数. 题目给出n个证据,m个子弹 Ai是证据.Bi是子弹.题目问:全部Bi对每一个Ai造成的伤害是多少,即每一个Bi在Ai中出现的次数总和. 解析: ...

  8. hdu -1251 统计难题(字典树水题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 建树之后 查询即可. G++提交 ME不知道为什么,c++就对了. 1 #include <iost ...

  9. 2019 杭电多校 HDU - 6625 three arrays 字典树+贪心

    题目链接:https://cn.vjudge.net/problem/HDU-6625 题意:a和b两个数组n个数,数字任意组合异或,求得到c数组的字典序最小 题解:对于两个数组从高位到低位建立两个字 ...

最新文章

  1. Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(andro
  2. android游戏开发学习笔记三(学习书籍 Android游戏编程之从零开始)
  3. go 切片slice删除元素的方法
  4. CDH-5.7.1离线安装
  5. css --- 使用媒体查询当屏幕宽度小于某个值时,隐藏掉某个类
  6. 【渝粤题库】陕西师范大学200131中国古代文论 作业(专升本)
  7. php yii 表单title,Yii2.0-ActiveForm表单结构自定义教程
  8. 如何在矩池云上安装java
  9. Windows中MySQL主从数据库搭建(一)
  10. 前端跨域解决方案总结
  11. TongWeb和Tomcat的区别
  12. 对于J2EE体系架构的理解
  13. 创建一个带目录的Word模板
  14. 你必须了解的支撑研究蛋白质组学的3大技术
  15. Word文档输入网址自动换行的解决办法
  16. npm init 自动创建 package.json 文件
  17. Bluetooth Battery Monitor(蓝牙电量监控软件)
  18. 服务器UDIMM、LRDIMM、RDIMM三种内存的区别
  19. UEFI Specification 第一章 引言(基于UEFI_Spec_2_9_2021_03_18)
  20. 如何将onedrive for business扩容为硬盘空间

热门文章

  1. 如何使用 System.IO 和 Visual C# 读取文本文件
  2. sql server数据库定时自动备份
  3. 使用MrBayes构建贝叶斯系统发育树【实践】
  4. 计算机的安全问题中保密是指,计算机安全保密练习册答案.doc
  5. 高度不定垂直居中_经典:CSS垂直居中的七种方法
  6. appender log4j 扩展_java-如何在log4j2中创建自定义Appender?
  7. 地区的json数据_Python 连接开放航空交通数据,轻松构建航班跟踪应用!
  8. java mybatis enum_mybatis处理枚举类的简单方法
  9. 19、计算机图形学——蒙特卡洛路径追踪
  10. log nginx 客户端请求大小_后端实践:Nginx日志配置(超详细)