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 ...
随机推荐
- 测试那些事儿—软测必备的Linux知识(三)
ubantu 打开命令提示符的方法:点击左上角图标输入terminal 1.系统管理 1.1 shutdown,系统关机命令 -h:关机 -r:重启 shutdown -h now 立即 ...
- python中的Matplot库和Gdal库绘制富士山三维地形图-参考了虾神的喜马拉雅山
首先请大家读一下面这篇文章了解什么是Gdal http://blog.csdn.net/grllery/article/details/77822595 剩下的我要公布绘制富士山的代码了,虽然基本co ...
- LDAP&it's usage
LDAP: 的英文全称是Lightweight Directory Access Protocol,简称为LDAP.LDAP是轻量目录访问协议[1],它是基于X.500标准的,但是简单多了并且可以根据 ...
- 防F12扒代码:按下F12关闭当前页面
只要按下那神秘的审查元素.按下那神秘的F12键,奇迹出现了!网站变成了空白页,可谓杀防君子放火必备佳品!一段JavaScript代码即可实现上述功能,插入到 footer.php 的 </bod ...
- 关于 legend_noa
真名:qlw 性别:男 常用ID:legend_noa(有时候也用fseject以表示我的弱, 曾经不懂事用goddess_Q),具体意思是我最喜欢的两个奥特曼:诺亚和雷杰多 p1 诺亚,p2 雷杰多 ...
- javascript DOM document属性
DOM控制页面中的所有元素 每个载入浏览器的HTML文档都会成为Document对象,利用它可对HTML页面中的所有元素进行访问 常用属性: title:返回或设置当前文档的标题 常用方法 write ...
- DevExpress 控件汉化代码和使用方法
DevExpress 第三方控件汉化的全部代码和使用方法 DevExpress.XtraEditors.Controls 此控件包中包含的控件最多,包括文本框,下拉列表,按钮,等等 ...
- 弹出的 Dialog 里,包含 Form,如何在关闭 Dialog 时,执行 resetFields(对整个表单进行重置,将所有字段值重置为初始值并移除校验结果)
做法: before-close 事件中,调用 resetFields 取消按钮事件中,调用 resetFields <el-dialog title="弹出窗口" :vis ...
- Hibernate运行原生sql并将查询的结果转化为对象
原生SQL查询执行的控制是通过SQLQuery接口进行的,通过执行Session.createSQLQuery()获取这个接口.下面来描述如何使用这个API进行查询.标量查询(Scalar queri ...
- JS中冒号的作用
JS中冒号的作用1.声明对象的成员2.switch语句分支3.三元表达式 1.声明对象的成员 var Book = { Name: '法', Price: 100, Discount : functi ...