[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 , 所以这道题只能用静态 ...
随机推荐
- MySQL常用方法
1.INSTR,例:INSTR(content,'\"SHOP_DES\"')>0,返回字符串在某一个字段的内容中第一次出现位置,没有为0 2.REPLACE,例:conte ...
- js修改css时如何考虑兼容性问题es5+es6
es5的写法 var elementStyle = document.createElement('div').style var vendor = (function(){ let transfor ...
- 【leetcode刷题笔记】Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [Codeforces 1139D] Steps to One
[题目链接] https://codeforces.com/contest/1139/problem/D [算法] 考虑dp 设fi表示现在gcd为i , 期望多少次gcd变为1 显然 , fi = ...
- 洛谷P2878 [USACO07JAN]保护花朵Protecting the Flowers
题目描述 Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. ...
- Mybatis新版实践
配置文件节点顺序 MyBatis的configuration节点需要有顺序,首先是propertes然后是settings,environment... @Param注解参数 对于Mapper接口,如 ...
- docker数据卷(volume)
1.什么是数据卷volume https://blog.csdn.net/dream_broken/article/details/52314993 为了了解什么是Docker Volume,首先我们 ...
- android获取时间差的方法
本文实例讲述了android获取时间差的方法.分享给大家供大家参考.具体分析如下: 有些时候我们需要获取当前时间和某个时间之间的时间差,这时如何获取呢? 1. 引用如下命名空间: import jav ...
- C++输入输出知识
1.strtok将字符串中的单词用' '分割出来 #include<iostream> #include<cstdio> #include<cstdlib> #in ...
- 文件异步上传,多文件上传插件uploadify
本文中使用java作为例子 uploadify下载 http://files.cnblogs.com/chyg/uploadify.zip jsp页面中需要引入: <script type=&q ...