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. CSS-动画巧用translated3d

    VUE2.0单元素或组件的过渡 情景:插件从右向左滑动,巧用translate3d .food position :fixed left : 0 top :0 bottom :48px z-index ...

  2. Python之简单验证码实现

    def v_code(): ret = '' for i in range(5): num = random.randint(0,9) alf = chr(random.randint(65,122) ...

  3. switch_case注意事项

    1.switch 语句有至少一个 case 代码块和一个可选的 default 代码块. 这里的 switch 从第一个 case 分支比较 a 的值,值为 3 匹配失败.然后比较 4.匹配,所以从  ...

  4. 软件测试_Fiddler抓包工具一

    多数资料摘抄至 https://www.cnblogs.com/miantest/p/7289694.html 一.在 macOS 下如何安装 (https://www.telerik.com/fid ...

  5. PAT乙级考前总结(五)

    字符串处理 1003 我要通过! (20 分) “答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否 ...

  6. Scrum学习心得

    一.Scrum学习心得: 最近简单的学习了一下scrum模式,感觉又开启了一个新世界的大门. 首先,scrum是一个应用于互联网研发的开发方式,这种开发方式的主要特点是快速迭代,持续交付. scrum ...

  7. Floyd(求每2个点之间的最短路)

    稍微改变即可求传递闭包,即关心两点之间是否有同路: for(int i=0;i<n;i++) for(int j=0;j<n;j++){ if(i==j) d[i][i]=0; else ...

  8. [USACO07OPEN]便宜的回文Cheapest Palindrome

    字串S长M,由N个小写字母构成.欲通过增删字母将其变为回文串,增删特定字母花费不同,求最小花费.        题目描述见上            显然 这是一道区间DP 从两头DP,枚举长度啥的很套 ...

  9. C语言入门(1)

    开始学习C语言 第一个C语言程序 #include<stdio.h> int main() { printf("Hello World!"); } C程序结构 1. 头 ...

  10. Alpha阶段个人心得

    在一个学期的学习中,我从一个只会一个人打打小规模代码的初学者也变成了一个能参与到团队做项目的“入门码农”,而我们团队从一开始对大型项目的望而生畏无从下手变成细分任务各司其职,也了解并感受到github ...