Phone List

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1075 1247 1800 1677 1298 
 

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的更多相关文章

  1. Trie的C++实现及HDU1251,hdu1671

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

  2. 3道入门字典树例题,以及模板【HDU1251/HDU1305/HDU1671】

    HDU1251:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题目大意:求得以该字符串为前缀的数目,注意输入格式就行了. #include<std ...

  3. HDU1671——前缀树的一点感触

    题目http://acm.hdu.edu.cn/showproblem.php?pid=1671 题目本身不难,一棵前缀树OK,但是前两次提交都没有成功. 第一次Memory Limit Exceed ...

  4. HDU1671 字典树

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

  5. hdu----(1671)Phone List(Trie带标签)

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

  6. HDU1671 - Phone List(Trie树)

    题目大意 给定一些电话号码,判断是否有电话号码是其他电话号码的前缀 题解 裸Trie树嘛~~~~只需要一个插入过程即可,假设X是Y的前缀,在插入的过程中有两种情况,X在Y之前插入,那么在插入Y的时候经 ...

  7. hdu1671字典树

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

  8. Phone List HDU1671

    字典树的包含与不包含关系 #include<bits/stdc++.h> using namespace std; ][]; ]; ; bool insert1( char *word ) ...

  9. HDU1671 水题字典树

    #include<cstdio> #include<cstdlib> #include<iostream> #include<cstring> #inc ...

随机推荐

  1. npm install 插件 --save与 --save -dev的区别

    npm i 插件   ,会把插件安装到node_modules目录中,不会修改package.json, npm i 插件 --save  ,项目发布上线之后还会依赖用到的插件,没有这些插件,项目不能 ...

  2. Vue基础之数据绑定

    我们学习一门新语言或者框架时,第一件事是什么呢,那必然是向世界say Hello. 创建一个Vue应用 话不多说,先上代码,让我们感受一下Vue的核心功能 <!DOCTYPE html> ...

  3. 整合spring+springmvc+mybatis

    开发环境: jdk 1.8 eclipse 4.7.0 (Oxygen) tomcat 8.5.29 mysql 5.7 开发前准备: spring 框架的jar包,在这里使用的是spring-5.0 ...

  4. YouTube数据:谁获得了最多订阅者?

    原文来源: https://www.kaggle.com/roshan77/youtube-data-who-got-the-most-subscribers 介绍: Python笔记 使用来自Soc ...

  5. Sql 无法解决 equal to 运算中 "Chinese_PRC_CI_AS" 和 "Chinese_PRC_90_CI_AI" 之间的排序规则冲突

    导致问题原因为创建时,表所使用的排序规则不一致 解决办法: 在对比条件后增加 collate Chinese_PRC_90_CI_AI 的转义即可 如: where test1.FieldName = ...

  6. 【模板】字符串匹配的三种做法(Hash、KMP、STL)

    题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 输入输出格式 输入格式: 第一行为一个字符串,即为s1 第二行为一个字符串,即为s2 输出格式: 1行 ...

  7. Android Studio学习之build.gradle文件

    参考书籍:第一行代码 最外层目录下的build.gradle buildscript{repositories{ jcenter() //代码托管仓库 } dependencies{ classpat ...

  8. visual studio 2017 中默认无法开发 Android 8.0 及以上系统的解决方案

    一般默认比较旧有两个原因,系统版本过旧,Visual Studio 版本过旧. 第一步,将windows 更新到最新版,必须是windows 10 并且更新到最新. 第二步,将visual studi ...

  9. sosreport-汇总收集linux系统信息

    有时候我们想将Linux服务器信息一次性查询并导出发送给他人,我们可以使用这个sosreport这个工具 安装  yum install sos -y 收集信息 sosreport 收集到的信息默认保 ...

  10. MySQL innobackupex全量备份恢复

    转自 http://blog.itpub.net/27099995/viewspace-1295099/ 先简单介绍一下这个工具:innobackupexinnobackupex比xtarbackup ...