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 ...
随机推荐
- Swift中enum, struct, class的有关使用方法
import Foundation print("Hello, World!") let a = var b = var c = a + b; c = //重载:函数名相同, 函数 ...
- 关于使用deepin在linux下安装mysql出现Can't connect to local MySQL server through socket '/tmp/mysql/mysql.sock' (2)的解决方法
根据目录/etc/mysql打开文件debain.cnf 此时文本里的内容为 # Automatically generated for Debian scripts. DO NOT TOUCH![c ...
- 2.3 i++/i--与++i/--i的运算
一.i++/i--: i先参与运算,运算完成后自加/减1: public class Test{ public static void main(String[] args){ // [1] ; i+ ...
- Teaching yourself programming -一个编程爱好者的碎碎念
多数时候,个人活动的展开都源于某个具体的动机.或许你是为了可以写点小工具,解决日常生活中的一些重复劳动:或许,你心怀梦想,梦想着某一天完成一款你心目中完美的游戏:又或许是,你内心憧憬电影里的hack, ...
- Centos7.4下安装Jumpserver 1.0.0(支持windows组件)
0)系统环境CentOS 7.4 IP: 192.168.100.10 [root@jumpserver-server ~]# cat /etc/redhat-release CentOS Linux ...
- C++插入排序
直接插入排序是一种简单的插入排序法,适用于少量数据的排序,是一种较为稳定的排序算法,本文通过插入排序的方法实现对一个数组进行从大到小和从小到大的排序. 1. 从小到大的插入排序: 例如:给定整型数组a ...
- Ubuntu 终端使用ss代理
用polipo软件,这个软件可以吧socket5转换成http代理 $ sudo apt-get install polipo $ sudo vim /etc/polipo/config 在文件中加入 ...
- Python assert(断言)
Python assert(断言)可以分别后面的判断是否正确,如果错误会报错 示例: a = 1 assert type(a) is int print('No problem') 输出结果: No ...
- Apache HTTP Server 与 Apache Tomcat 的区别
要明白他们之间的区别,我们首先需要明白HTTP协议.HTML页面.JSP.Servlet之间的区别和联系. HTTP协议是在TCP/IP协议之上的应用层协议,用以在客户端和服务器之间传递信息.一般传递 ...
- idea 的方法上面注释在格式化后换行问题
通过/**生成的方法上面的模板样式是: /** * 楼盘 * @param build * @return */ 格式化代码后是: /** * 楼盘 * * @param build * @retur ...