[ACM] hdu 1671 Phone List (字典树)
来源:互联网 发布:经济数据公布时间表 编辑:程序博客网 时间:2024/05/17 23:53
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.
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
2391197625999911254265113123401234401234598346
Sample Output
NOYES
Source
2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)
解题思路:
判断输入的串中是否存在某个串是另外串的前缀。建立字典树,关键是设立某个串结尾的标志,即在哪个字母结尾。要判断是否存在前缀要考虑两种情况,一是当前输入的串是否是以前输入的串的前缀,而是以前输入的串是否是当前输入的串的前缀。前一种情况在查找该串时,会从第一个字母查找到最后一个字母,中间不返回任何值,说明当前的串是以前的前缀,后一种情况,当在查找当前串的时候遇到以前串结束的标志,则说明以前串是当前串的前缀。代码中结束的标志为保存串中最后一个字母的那个节点的cnt值为-1.
如图:
代码:
0 0
- [ACM] hdu 1671 Phone List (字典树)
- [ACM] hdu 1671 Phone List (字典树)
- hdu 1671 Phone List(字典树)
- HDU-#1671 Phone List(字典树)
- hdu 1671 phone list(字典树)
- HDU 1671 Phone List(字典树)
- HDU 1671 Phone List(字典树)
- HDU 1671 Phone List(字典树)
- HDU 1671 Phone List(字典树)
- HDU 1671 Phone List (字典树)
- HDU 1671 Phone List(字典树)
- hdu 1671 Phone List(字典树)
- HDU 1671 Phone List (字典树)
- HDU 1671 Phone List(字典树)
- hdu 1671 Phone List(字典树)
- HDU 1671 Phone List(字典树)
- 杭电acm 1671Phone List(字典树)
- hdu 1671 Phone List 字典树
- hdu 2689 Sort it 一维树状数组的应用
- [ACM] hdu 1251 统计难题 (字典树)
- [笔记] Week 1: Social Perceptions and Misperceptions
- B树 B+树
- 时间不是问题!在这里你就可以实现它!
- [ACM] hdu 1671 Phone List (字典树)
- 2015/8/10/数据库基本操作/单例
- [HTTP那些事]超大JSON文本
- poj 1068 Parencodings
- [ACM] POJ 2418 Hardwood Species (Trie树或者map)
- 安卓手机的APP图标尺寸规范和图标命名规范
- 解决Unsupported major.minor version 51.0错误
- hadoop配置
- 两道笔试编程题(求水仙数和求平方根数组之和)