HDU 1671 Phone List (Trie)
pid=1671">Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12879 Accepted Submission(s): 4391
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.
on each line. A phone number is a sequence of at most ten digits.
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
NO
YES
解析:求是否为非前缀串(即随意串的前缀都不是一个已有的串)。在建Trie树时,仅仅标记串的最后一个字符,然后查询时,看每一个串的中间是否存在被标记的点就可以。
AC代码:
#include <bits/stdc++.h>
using namespace std; const int maxnode = 100000 * 10 + 5;
const int sigma_size = 10; struct Trie{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz; void clear(){ sz = 1; memset(ch[0], 0, sizeof(ch[0])); } //初始仅仅有一个根节点
int idx(char c){ return c - '0'; } void insert(string s){
int u = 0, n = s.size();
for(int i=0; i<n; i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] ++; //仅仅标记串尾的字符
} bool find(string s){
int u = 0, n = s.size();
for(int i=0; i<n; i++){
int c = idx(s[i]);
if(val[u]) return false; //在一个串的中间出现了已经标记的点
u = ch[u][c];
}
return true;
} }; Trie T;
vector<string> S; int main(){
#ifdef sxk
freopen("in.txt", "r", stdin);
#endif // sxk int t, n;
string s;
scanf("%d", &t);
while(t--){
S.clear();
T.clear(); //不要忘了初始化
scanf("%d", &n);
for(int i=0; i<n; i++){
cin>>s;
T.insert(s);
S.push_back(s);
}
int cnt = S.size();
int i;
for(i=0; i<cnt; i++){
if(!T.find(S[i])) break;
}
puts(i < cnt ? "NO" : "YES");
}
return 0;
}
HDU 1671 Phone List (Trie)的更多相关文章
- HDU 1671 Phone List(Trie的应用与内存释放)
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU 2846:Repository(Trie)
http://acm.hdu.edu.cn/showproblem.php?pid=2846 题意:给出N个模式串,再给出M个文本串,问每一个文本串在多少个模式串中出现. 思路:平时都是找前缀的,这里 ...
- HDU 1251-统计难题(Trie)
题意: 给一组单词 开始提问每次给一个串求该串是上面几个单词的前缀 分析: 没给数据规模,但用链表写ME好几次,又用数组写开小RE了,试了几次才过了,真是醉了... #include <map& ...
- HDU 1251 统计难题 (Trie)
pid=1251">统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/ ...
- HDU 5938 Four Operations(四则运算)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 5775 Bubble Sort(冒泡排序)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 1005 Number Sequence(数列)
HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDU 1176 免费馅饼 (动态规划)
HDU 1176 免费馅饼 (动态规划) Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼 ...
随机推荐
- 蓝桥杯-算法训练--ALGO-4 结点选择
本人是一个刚刚接触C++不久的傻学生~记录一些自己的学习过程.大神路过可以批评指正~ 刚学动态规划,水平还很渣,一下子不知道从何下手,借鉴了一下这位大哥的文章 http://www.cnblogs.c ...
- onload、DOMContentLoaded与性能问题
onload.DOMContentLoaded与性能问题 onload事件 DomContentLoaded 1.onload事件 onload事件一般在所有的文档内容加载完成后触发,如果网页中图 ...
- jQuery_事件学习
一.click事件 click事件----鼠标单击事件 $('.bt').click(function() { alert("本身的事件");}) 当class为bt的div被但单 ...
- 【吐槽】关于256个 class可以覆盖一个id的问题
还是说今天下午面试的事情,被面试官问了 40多分钟的问题,我觉得丫 一定是从哪个网站down了几份面试题,自个儿整合了一下,然后挨个问,刚开始感觉哟,不错哦,面试官懂的蛮多的. 然后问到某个问题之后, ...
- 机器学习,安装python的支持包
windows10,64位: 以下命令行安装均在python目录下,对应的whl文件也被我拷贝到python目录下: http://www.lfd.uci.edu/~gohlke/pythonlibs ...
- C#写的较完美验证码通用类
using System; using System.Collections; using System.ComponentModel; using System.Data; using System ...
- 使用MS Test做单元测试
声明:本篇博客翻译自:http://www.c-sharpcorner.com/article/unit-testing-with-ms-tests-in-c-sharp/ 写在翻译之前: 依然清晰的 ...
- css3 ajax加载进度线
最近想了想ajax加载时的进项,便着手写了这个,我想css3的支持度已经够了 <button onclick="start()">button</button&g ...
- unique & lower_bound C++
原来C++也有unique和lower_bound,只需头文件iostream unique unique可以对数组进行相邻元素的"去重",实现效果是把所有不重复的元素按顺序放在数 ...
- MyBatis《1》
MyBatis入参考文档:http://mybatis.org/mybatis-3/zh/ 1.使用MyBatis前的准备 1.增加Maven依赖 <dependency> <g ...