HDU1671 Phone List
Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
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.
3
911
97625999
91125426
5
113
12340
123440
12345
98346
YES
Statistic | Submit | Discuss | Note
http://acm.hdu.edu.cn/showproblem.php?pid=1671
trie树做法:
#include<bits/stdc++.h> using namespace std; int n, t;
bool ans;
const int MAXN = ;
int g[MAXN][], f[MAXN], gx, w[MAXN]; bool add (int u, string s, int x) {
if (x >= s.length()) return ;
w[u] = ;
if (f[g[u][s[x] - '']]) {
return ;
}
else {
if (!g[u][s[x] - '']) g[u][s[x] - ''] = ++gx;
if (x == s.length() - ) {
if (w[g[u][s[x] - '']]) return ;
f[g[u][s[x] - '']] = ;
w[g[u][s[x] - '']] = ;
return ;
}
else {
return add(g[u][s[x] - ''], s, x + );
}
}
} int main() {
cin >> t;
while (t--) {
ans = ;
cin >> n;
memset(f, , sizeof(f));
memset(g, , sizeof(g));
memset(w, , sizeof(w));
gx = ;
while (n--) {
string s;
cin >> s;
ans = min(ans, add(, s, ));
}
if (ans) {
cout << "YES\n";
}
else {
cout << "NO\n";
}
}
return ;
}
排序,每对相邻的暴力验证一下:
#include<bits/stdc++.h>
using namespace std;
string s[];
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = ; i <= n; ++i) cin >> s[i];
sort(s + , s + + n);
bool tf = ;
for (int i = ; i <= n && tf; ++i) {
if (s[i].length() > s[i - ].length()) {
bool x = ;
for (int j = ; j < s[i - ].length(); ++j) {
if (s[i - ][j] != s[i][j]) {
x = ;
break;
}
}
if (x) tf = ;
}
}
if (tf) cout << "YES\n";
else cout << "NO\n";
}
return ;
}
HDU1671 Phone List的更多相关文章
- Trie的C++实现及HDU1251,hdu1671
#include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...
- 3道入门字典树例题,以及模板【HDU1251/HDU1305/HDU1671】
HDU1251:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题目大意:求得以该字符串为前缀的数目,注意输入格式就行了. #include<std ...
- HDU1671——前缀树的一点感触
题目http://acm.hdu.edu.cn/showproblem.php?pid=1671 题目本身不难,一棵前缀树OK,但是前两次提交都没有成功. 第一次Memory Limit Exceed ...
- HDU1671 字典树
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu----(1671)Phone List(Trie带标签)
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU1671 - Phone List(Trie树)
题目大意 给定一些电话号码,判断是否有电话号码是其他电话号码的前缀 题解 裸Trie树嘛~~~~只需要一个插入过程即可,假设X是Y的前缀,在插入的过程中有两种情况,X在Y之前插入,那么在插入Y的时候经 ...
- hdu1671字典树
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Phone List HDU1671
字典树的包含与不包含关系 #include<bits/stdc++.h> using namespace std; ][]; ]; ; bool insert1( char *word ) ...
- HDU1671 水题字典树
#include<cstdio> #include<cstdlib> #include<iostream> #include<cstring> #inc ...
随机推荐
- JDK无法卸载问题解决
在控制面板卸载JDK时,显示正在收集删除文件,进度条满了之后就闪退了,但JDK还在,试了几次都是如此. 后来,发现微软官方出了“修复阻止程序安装或删除的问题”的应用,可以自动修复包括阻止你安装或删除程 ...
- python基础(六)
一.内置函数 # input()# type()# len()# print()# enumerate()# list()# dict()# tuple()# set()# str()# int()# ...
- 关于mdb数据库在插入过程中报错->Syntax error in INSERT INTO statement.(sql语句没问题)
今天,在做mdb数据库的增删改查的时候,代码报错插入语句有问题,但是在数据库中正常执行,苦苦探索了多次,终于找到了问题所在. 结果如图: 上面是报错 下面是解决方案 解决方案:主要原因是offic ...
- JavaScript基础之对象属性的检测和枚举
属性检测 对象作为属性的集合,属性又包括自有属性和继承属性: 检测方法: \__ in运算符: \__ var obj = { x:1 } console.log( 'toString' in o ...
- 使用django发布带图片的网页(上)
如何使用django发布带静态图片的网页呢? 在settings.py里加入下面内容 STATIC_ROOT = os.path.join(os.path.dirname(__file__),'sta ...
- 入学java的第一天
登录http://www.oracle.com,下载JDK(J2SE) JDK 1.0,1.1,1.2,1.3,1.4 1.5(JDK5.0) 支持注解.支持泛型 1.6(JDK6.0)Server2 ...
- centos7进单用户
当重启linux系统,进入系统选择页面的时候,按e 在linux16那一行最后面添加 init=/bin/sh 按ctrl+c 挂载根分区,可读写 mount / -o rw, remount
- RABBITMQ too many heartbeats missed
执行rabbitmqctl status | grep -A 4 file_descriptors 显示socket_used 达到 socket_limited 的值 增加socket_limi ...
- Ubuntu 16.04 LTS 安装 miniconda
Ubuntu 16.04 LTS 安装 miniconda 下载 miniocnda 的 bash 文件下载链接 https://conda.io/miniconda.html ,我选择的是 64-b ...
- [IOS][已越狱]配合网易云音乐,使用Bridge快速免iTunes导入音乐到“音乐”
前置:已越狱IOS设备 工具表: Bridge{bigboss源,可以使用cydown以非官方服务器下载安装} 网易云音乐{Appstore} Filza{很多源都有} 网易云试听缓存路径 ...