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. 转发: 探秘Java中的String、StringBuilder以及StringBuffer

    原文地址 探秘Java中String.StringBuilder以及StringBuffer 相信String这个类是Java中使用得最频繁的类之一,并且又是各大公司面试喜欢问到的地方,今天就来和大家 ...

  2. Spring+Mybatis+SpringMVC+Maven+MySql搭建实例(转)

    http://blog.csdn.net/evankaka/article/details/48785513?spm=5176.100239.blogcont28591.10.9Fdj9R

  3. 最基础的 swift 语言

    import Foundation //打印函数 print("Hello, World!") //不用加分号, 字符串就是"", 不用加@ print(&qu ...

  4. Maven配置pom.xml,正在下载时网络不佳下载失败的解决方案

    环境:jdk1.7.0_17,Myeclipse 10,apache-maven-3.2.5 配置项目中pom.xml的dependencies时 ,如果本地仓库没有的话,就会自动下载.找不到仓库位置 ...

  5. PTA1

    1-1 数组定义中,数组名后是用方括号括起来的常量表达式,不能用圆括号. (1分) [T ] F 1-2 在C语言中能逐个地使用下标变量,也能一次引用整个数组. (1分) T [F]因为它有首地址 1 ...

  6. Error: no override found for 'vtkRenderWindow'

    VSK7.1+QT5.10环境下报该错误,应该在mainwindow.cpp中添加如下语句 记住,是在mainwindow.cpp中添加,不是在main.cpp中添加:

  7. ionic上拉加载组件 ion-infinite-scroll自动调用多次的问题

    参考文章地址:http://www.cnblogs.com/luleixia/p/6402418.html ionic 一个上拉刷新的组件 ion-infinite-scroll,如果页面未填充满页面 ...

  8. ios-Nav右上角按钮

    右上角的设置按钮 //****************** 右上角保存按钮 ****************** UIButton *rightBtn = [UIButton buttonWithTy ...

  9. CentOS6/7快捷使用gcc5

    Centos6/7自带的gcc为4.x版本,可通过devtoolset工具集安装gcc5.x版本 1. 添加yum源 1)CentOS6 [hhorak-devtoolset--rebuild-boo ...

  10. java中构造代码块、方法调用顺序问题

    1. 继承的概念 继承在本职上是特殊——一般的关系,即常说的is-a关系.子类继承父类,表明子类是一种特殊的父类,并且具有父类所不具有的一些属性或方法. 2. 继承中的初始化顺序 从类的结构上而言,其 ...