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

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

解析:求是否为非前缀串(即随意串的前缀都不是一个已有的串)。在建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)的更多相关文章

  1. HDU 1671 Phone List(Trie的应用与内存释放)

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. HDU 2846:Repository(Trie)

    http://acm.hdu.edu.cn/showproblem.php?pid=2846 题意:给出N个模式串,再给出M个文本串,问每一个文本串在多少个模式串中出现. 思路:平时都是找前缀的,这里 ...

  3. HDU 1251-统计难题(Trie)

    题意: 给一组单词 开始提问每次给一个串求该串是上面几个单词的前缀 分析: 没给数据规模,但用链表写ME好几次,又用数组写开小RE了,试了几次才过了,真是醉了... #include <map& ...

  4. HDU 1251 统计难题 (Trie)

    pid=1251">统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/ ...

  5. HDU 5938 Four Operations(四则运算)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  6. HDU 5775 Bubble Sort(冒泡排序)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  7. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  8. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

  9. HDU 1176 免费馅饼 (动态规划)

    HDU 1176 免费馅饼 (动态规划) Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼 ...

随机推荐

  1. [Bayesian] “我是bayesian我怕谁”系列 - Boltzmann Distribution

    使用Boltzmann distribution还是Gibbs distribution作为题目纠结了一阵子,选择前者可能只是因为听起来“高大上”一些.本章将会聊一些关于信息.能量这方面的东西,体会“ ...

  2. AngularJS学习篇(十八)

    AngularJS API AngularJS 全局 API 用于执行常见任务的 JavaScript 函数集合,如: 比较对象 迭代对象 转换对象 全局 API 函数使用 angular 对象进行访 ...

  3. this指向(匿名函数问题)

    1.匿名函数中 this一般指向window对象 2.闭包函数中的this,指向window var mod = { init: function(){ console.log('this',this ...

  4. Python爬虫入门:综述

    大家好哈,最近博主在学习Python,学习期间也遇到一些问题,获得了一些经验,在此将自己的学习系统地整理下来,如果大家有兴趣学习爬虫的话,可以将这些文章作为参考,也欢迎大家一共分享学习经验. Pyth ...

  5. Python做的第一个小项目-模拟登陆

    1. 用户输入帐号密码进行登陆 2. 用户信息保存在文件内 3. 用户密码输入错误三次后锁定用户 主要采用循环语句和条件语句进行程序流程的控制,加入文件的读写操作 while True: choice ...

  6. 使用 gulp-file-include 构建前端静态页面

    前言 虽然现在单页面很流行,但是在 PC 端多页面还是常态,所以构建静态页面的工具还有用武之地.最近也看到了一些询问如何 include HTML 文件的问题. 很多时候我们在写静态页面的时候也希望能 ...

  7. java三大框架项目和Redis组合使用

    已知一个已有的Struts+Spring+Hibernate项目,以前使用MySQL数据库,现在想把Redis也整合进去.1. 相关Jar文件 下载并导入以下3个Jar文件: commons-pool ...

  8. 结合GET(),POST()实现一个简单、完整的服务器

    复习一下: 基础模块 作用 fs fs模块用于对系统文件及目录进行读写操作 http 创建服务器.e.g.http.createServer(); queryString 把url带的参数串转化为数组 ...

  9. javaScript额外笔记

    --------------------------------------------------------Part 1javascript:脚本语言辅助开发:网页的前台开发三大块:1.HTML ...

  10. 一篇文章让你明白python的装饰器

    在看闭包问题之前先来看看关于python中作用域的问题 变量作用域 对于上述代码中出现错误,肯定没什么疑问了,毕竟b并没有定义和赋值,当我们把代码更改如下后: 再看一个例子: 首先这个错误已经非常明显 ...