[hdu 1671] Phone List - Trie
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.
InputThe 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.OutputFor 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 题意:判断某个字符串是全部字符串中的某个字符串的前缀,有输出YES, 反之NO
思路:利用Trie来做,可以变插入变检测,如果在插入过程中有标记节点,则之前某个字符串是该字符串的前缀,如果插入完成之后,其后有子数,则该字符串是某个字符串的前缀。
每组Case做完后,要释放空间,再建立新树。
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
#define Max 10
typedef struct TrieNode *Trie;
struct TrieNode
{
Trie next[Max];
int cnt;
TrieNode()
{
for (int i = ; i < Max; i++) {
next[i] = NULL;
cnt = ;
}
}
}; bool Insert(Trie root, char *s)
{
Trie p = root;
for (int i = ; s[i]; i++) {
int t = s[i]-'';
if (p->next[t] == NULL)
p->next[t] = new TrieNode;
p = p->next[t];
if (p->cnt >= ) return ;
}
p->cnt++;
for (int i = ; i < Max; i++) {
if (p->next[i] != NULL)
return ;
}
return ;
} void FreeTrie(Trie root)
{
for (int i = ; i < Max; i++) {
if (root->next[i] != NULL)
FreeTrie(root->next[i]);
}
delete root;
} int main()
{
//freopen("1.txt", "r", stdin);
int T;
scanf("%d", &T);
while (T--) {
int n;
char s[][];
scanf("%d", &n);
Trie root = new TrieNode;
int flag = ;
for (int i = ; i < n; i++) {
scanf("%s", s[i]);
if (Insert(root, s[i]))
flag = ;
} if (flag) printf("NO\n");
else printf("YES\n");
FreeTrie(root);
} return ;
}
[hdu 1671] Phone List - Trie的更多相关文章
- HDU 1671 Phone List (Trie·数组实现)
链接:http://blog.csdn.net/acvay/article/details/47089657 题意 给你一组电话号码 判断其中是否有某个电话是另一个电话的前缀 字典树的基础应用 ...
- hdu 1671 Phone List (Trie树)
简单的字典树应用,在建树的时候判断就行了. 需要注意的语法: 在使用malloc和free来处理动态内存的时候,仅仅是释放了这个对象所占的内存,而不会调用这个对象的析构函数:使用new和delete就 ...
- hdu 1671&& poj 3630 (trie 树应用)
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25280 Accepted: 7678 Descr ...
- HDU 1671 Phone List(Trie的应用与内存释放)
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 1671 Phone List 字典树
// hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...
- hdu 4825 Xor Sum(trie+贪心)
hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...
- HDU 1671 Phone List (Trie)
pid=1671">Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 1671 (字典树统计是否有前缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...
- POJ 3630 , HDU 1671 Phone List - from lanshui_Yang
这道题也是一道找前缀的问题,很自然地要用到Trie树,但是如果用动态Trie树(即用指针开辟内存)的话,虽然在HDU上可以过(可能是HDU的数据比较水),但在POJ上会TLE , 所以这道题只能用静态 ...
随机推荐
- 分享知识-快乐自己:虚拟机 NET 网络配置
第一步: 第二步: 第三步: 第四步: 第五步: 第六步: 第七步: 第九步: 第十步: 第十一步: 第十二步: 第十三步: 成功. 第十四步:开启开机自启动网路连接 cd /etc/sy ...
- 5 Python 数据类型—数字
Python Number 数据类型用于存储数值. 数据类型是不允许改变的,这就意味着如果改变 Number 数据类型的值,将重新分配内存空间. var1 = 1 var2 = 10 您也可以使用de ...
- Oracle学习笔记_03_单行函数
1.SQL函数: 单行函数.多行函数 单行函数: 操作数据对象 接受参数返回一个结果 只对一行进行变换 每行返回一个结果 可以转换数据类型 ...
- php 冒泡排序原理
$start = microtime(true); $popArr = array(6,3,23,1,5,100,399,99,66); echo '6,3,23,1,5,100,399,99 ...
- 关于DLL调试的两个工具(dependency walker和dumpbin.exe)
Dependency Walker工具: 该工具用来查看dll依赖库和依赖库内的函数. 左上角:模块依赖树,表示该dll所依赖的其它dll文件 右上角:导入函数,即与当前根模块有关系的函数(即根模块或 ...
- codeforces 637A A. Voting for Photos(水题)
题目链接: A. Voting for Photos time limit per test 1 second memory limit per test 256 megabytes input st ...
- OpenCV——饱和度调整
参考: 闲人阿发伯的博客 // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED ...
- bzoj 1441: Min 裴蜀定理
题目: 给出\(n\)个数\((A_1, ... ,A_n)\)现求一组整数序列\((X_1, ... X_n)\)使得\(S=A_1*X_1+ ...+ A_n*X_n > 0\),且\(S\ ...
- poj2420 A Star not a Tree? 模拟退火
题目大意: 给定n个点,求一个点,使其到这n个点的距离最小.(\(n \leq 100\)) 题解 模拟退火上 #include <cmath> #include <cstdio&g ...
- 一:ORM关系对象映射(Object Relational Mapping,简称ORM)
狼来的日子里! 奋发博取 10)django-ORM(创建,字段类型,字段参数) 一:ORM关系对象映射(Object Relational Mapping,简称ORM) ORM分两种: DB fir ...