POJ 3630 Phone List 【Trie树入门】
2013-04-18 19:39
726 查看
Phone List
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:
Emergency 911
Alice 97 625 999
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 nlines 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
Sample Output
Source
Nordic 2007
算法:Trie 前缀树入门
思路:1.Trie树版本【lrj《算法竞赛入门经典 训练指南 P208 - 209》】
每次输入一个字符串,则插入 Trie 树中。
边插入边判断 ,那么会出现下面的三种情况:
第一、当前插入的字符串从来没有被插入过,返回 false表示插入成功,继续插入下一条字符串
第二、当前插入的字符串是已经插入过的字符串的前缀,停止插入,返回 true 输出 NO。
第三、当前插入的字符的前缀已经作为单独的字符串插入过,停止插入,返回 true 输出 NO。
2.ORC的 C++ STL版本,思路简单,代码清晰。
先用 vector<string> 存储输入的每条字符串
再用 sort 从大到小排序
最后依次遍历看后面的字符串中是否能 find() 前一个
【Trie树版本】
【ORC的 C++ STL版本】
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18122 | Accepted: 5769 |
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:
Emergency 911
Alice 97 625 999
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 nlines 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
Nordic 2007
算法:Trie 前缀树入门
思路:1.Trie树版本【lrj《算法竞赛入门经典 训练指南 P208 - 209》】
每次输入一个字符串,则插入 Trie 树中。
边插入边判断 ,那么会出现下面的三种情况:
第一、当前插入的字符串从来没有被插入过,返回 false表示插入成功,继续插入下一条字符串
第二、当前插入的字符串是已经插入过的字符串的前缀,停止插入,返回 true 输出 NO。
第三、当前插入的字符的前缀已经作为单独的字符串插入过,停止插入,返回 true 输出 NO。
2.ORC的 C++ STL版本,思路简单,代码清晰。
先用 vector<string> 存储输入的每条字符串
再用 sort 从大到小排序
最后依次遍历看后面的字符串中是否能 find() 前一个
【Trie树版本】
3630 | Accepted | 2576K | 125MS | C++ | 1421B | 2013-04-18 19:21:01 |
A | Accepted | 2576 KB | 110 ms | C++ | 1282 B | 2013-04-18 18:54:47 |
#include<stdio.h> #include<string.h> const int maxnode = 10000*10+10; struct Trie{ int ch[maxnode][10]; int val[maxnode]; int sz; void init() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); } bool insert(char *s, int v){ int len = strlen(s); int u = 0; int id, i; for(i = 0; i < len; i++){ id = s[i]-'0'; if(!ch[u][id]){ val[sz] = 0; memset(ch[sz], 0, sizeof(ch[sz])); ch[u][id] = sz++; } u = ch[u][id]; if(val[u]) return true; //当前插入的 s的前缀已经单独插入过 else if(i == (len-1)){ //如果当前插入的 s 是已经插入过的字符串的前缀 for(int j = 0; j <= 9; j++) if(ch[u][j]) return true; }//else if(i == (len-1) && !val[u]) return true; } val[u] = v; return false; //成功插入 } }trie; int main() { int T; scanf("%d", &T); while(T--) { int n; char str[11]; bool flag = true; scanf("%d", &n); trie.init(); while(n--) { scanf("%s", str); //插入时判断,一旦冲突则退出 if(trie.insert(str, 1)) { flag = false; break; } } if(n > 1) while(n--) scanf("%s", str); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
【ORC的 C++ STL版本】
Accepted | 848K | 438MS | C++ | 714B | 2013-04-18 00:56:21 |
#include<string> #include<vector> #include<algorithm> #include<iostream> using namespace std; int main() { int T; cin>>T; while(T--) { int n; vector<string> v; string s; cin>>n; v.clear(); for(int i = 0; i < n; i++) { cin>>s; v.push_back(s); } sort(v.begin(), v.end()); bool flag = true; for(int i = 0; i < n-1; i++) { if(v[i+1].find(v[i]) == 0) { flag = false; break; } } if(flag) cout<<"YES\n"; else cout<<"NO"<<endl; } return 0; }
相关文章推荐
- POJ 3630 Phone List 【Trie树入门】
- poj 3630 Phone List Trie树
- POJ-3630-Phone List-Trie树(键树、数字查找树、基数树)
- POJ 3630:Phone List(字典树入门)
- poj 3630 Phone List trie树—字典树 静态数组版!!!
- HDU 1671 && POJ 3630 Phone List (Trie树 好题)
- POJ 3630 - Phone List (Trie树)
- POJ-3630 Phone List Trie树 | qsort
- poj_3630 Phone List(Trie树练习)
- POJ_3630_Phone List_Trie树
- 【原】 POJ 3630 Phone List Trie树 解题报告
- POJ 3630 Phone List(trie树的简单应用)
- Trie树学习小记 Poj 3630 & Hdu1671 Phone List
- poj 3630 || zoj 2876 ||hdu 1671 Phone List (Trie树的应用)
- poj 3630 Phone List trie树
- POJ 3630 Phone List(Trie树,静态数组实现)
- POJ 3630 && HDU 1671 Phone list(静态字典树)
- POJ 3630 Phone List
- POJ3630 Phone List 题解&代码
- poj 1056 IMMEDIATE DECODABILITY & poj 3630 Phone List