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. 蓝桥杯-算法训练--ALGO-8 操作格子

    问题描述 有n个格子,从左到右放成一排,编号为1-n. 共有m次操作,有3种操作类型: 1.修改一个格子的权值, 2.求连续一段格子权值和, 3.求连续一段格子的最大值. 对于每个2.3操作输出你所求 ...

  2. linux环境手动编译安装Nginx实践过程 附异常解决

    1.下载nginx源码包并解压 可在http://nginx.org/en/download.html下载.tar.gz的源码包,如(nginx-1.4.7.tar.gz) 或者使用云盘下载   ht ...

  3. Spring各jar包作用及依赖

    先附spring各版本jar包下载链接http://repo.spring.io/release/org/springframework/spring/ spring.jar 是包含有完整发布模块的单 ...

  4. (11.13)Java小知识!

    今天想要与大家分享一下有关于构造方法的知识! 构造方法的定义与作用 构造方法是一种特殊类型的方法.当一个对象被创建的时候,构造方法用来初始化对象,也就是说构造方法其实是一个名词而不是动词,像我刚刚开始 ...

  5. ab使用命令

    ab使用-A auth-username:password    向服务器提供基本认证信息.用户名和密码之间":"分割,以base64编码形式发送.无论服务器是否需要(即是否发送了 ...

  6. 图片验证码的JAVA工具类

    我们平时开发时经常会遇到需要图片验证码,基础的验证码包括了数字.字母.甚至可能有汉字.下面我给出一个简单的工具类. package com..ankang.tony.util; import java ...

  7. mac电脑安装apache,不能启动

    因为mac系统是自带apach的 如果安装正确还是不能启动,有可能是 mac电脑自带apache功能,与安装的apache冲突. 这样关闭Mac自带apach即可. mac电脑apache命令:重启a ...

  8. 史上最全的IntelliJIdea快捷键

    Ctrl+Shift+方向键Up/Down 代码向上/下移动. Ctrl+X 删除行 Ctrl+Y 也是删除行,不知道有啥区别 Ctrl+D 复制行 Ctrl+Alt+L 格式化代码 Ctrl+N 查 ...

  9. python进阶------进程线程(五)

    Python中的IO模型 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别 ...

  10. MySQL运维相关工具汇总(待补充)

    1.orztop查看show full processlist http://hidba.org/?p=841 2.orzdba查看系统状态信息 http://code.taobao.org/p/or ...