POJ 3630 - Phone List (Trie)
来源:互联网 发布:编程什么意思 编辑:程序博客网 时间:2024/06/06 01:22
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 withn, the number of phone numbers, on a separate line, 1 ≤ n ≤ 10000. Then followsn 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
2391197625999911254265113123401234401234598346
Sample Output
NOYES
Source
题意:
判断输入的电话号码中是否有电话号码是另外号码的前缀
思路:
Trie 但是不能直接插入判断 比如
2
1234
123
一种解决方法是按大小排序 然后依次插入判断
#include <stdio.h>#include <iostream>#include <vector>#include <algorithm>#include <cstring>#include <string>#include <map>#include <cmath>#include <queue>#include <set>using namespace std;//#define WIN#ifdef WINtypedef __int64 LL;#define iform "%I64d"#define oform "%I64d\n"#define oform1 "%I64d"#elsetypedef long long LL;#define iform "%lld"#define oform "%lld\n"#define oform1 "%lld"#endif#define S64I(a) scanf(iform, &(a))#define P64I(a) printf(oform, (a))#define S64I1(a) scanf(iform1, &(a))#define P64I1(a) printf(oform1, (a))#define FOR(i, s, t) for(int (i)=(s); (i)<(t); (i)++)const int INF = 0x3f3f3f3f;const double eps = 10e-9;const double PI = (4.0*atan(1.0));const int maxsize = 200000;struct Trie { int size; int val[maxsize]; int ch[maxsize][10]; Trie() { clear(); } void clear() { size = 1; memset(ch[0], 0, sizeof(ch[0])); } int idx(char c) { return c - '0'; } bool insert(const char * s, int v) { int len = strlen(s); int u = 0; for(int i=0; i<len; i++) { int c = idx(s[i]); if(!ch[u][c]) { memset(ch[size], 0, sizeof(ch[size])); val[size] = 0; ch[u][c] = size++; } if(val[u]) return false; u = ch[u][c]; } val[u] = v; return true; }};char s[20];Trie trie;string ss[10000+20];int main() { int T; scanf("%d", &T); while(T--) { int n; trie.clear(); scanf("%d", &n); for(int i=0; i<n; i++) { cin>>ss[i]; } sort(ss, ss+n); int ans = 1; for(int i=0; i<n; i++) { if(ans && !trie.insert(ss[i].c_str(), 1)) { ans = 0; } } if(ans) puts("YES"); else puts("NO"); } return 0;}
另外一种方法是给每个单词的路径标记一下 然后插入的时候判断一下就行了
#include <stdio.h>#include <iostream>#include <vector>#include <algorithm>#include <cstring>#include <string>#include <map>#include <cmath>#include <queue>#include <set>using namespace std;//#define WIN#ifdef WINtypedef __int64 LL;#define iform "%I64d"#define oform "%I64d\n"#define oform1 "%I64d"#elsetypedef long long LL;#define iform "%lld"#define oform "%lld\n"#define oform1 "%lld"#endif#define S64I(a) scanf(iform, &(a))#define P64I(a) printf(oform, (a))#define S64I1(a) scanf(iform1, &(a))#define P64I1(a) printf(oform1, (a))#define FOR(i, s, t) for(int (i)=(s); (i)<(t); (i)++)const int INF = 0x3f3f3f3f;const double eps = 10e-9;const double PI = (4.0*atan(1.0));const int maxsize = 200000;struct Trie { int size; int val[maxsize]; int ch[maxsize][10]; Trie() { clear(); } void clear() { size = 1; memset(ch[0], 0, sizeof(ch[0])); } int idx(char c) { return c - '0'; } bool insert(const char * s) { int len = strlen(s); int u = 0; for(int i=0; i<len; i++) { int c = idx(s[i]); if(!ch[u][c]) { memset(ch[size], 0, sizeof(ch[size])); val[size] = 0; ch[u][c] = size++; } if(val[u] == 2) return false; val[u] = 1; u = ch[u][c]; } if(val[u]) return false; val[u] = 2; return true; }};char s[100];Trie trie;int main() { int T; scanf("%d", &T); while(T--) { int n; trie.clear(); scanf("%d", &n); int ans = 1; for(int i=0; i<n; i++) { scanf("%s", s); if(ans && !trie.insert(s)) { ans = 0; } } if(ans) puts("YES"); else puts("NO"); } return 0;}
- poj 3630 Phone List(依旧Trie~)
- POJ 3630 - Phone List (Trie)
- POJ 3630 - Phone List (Trie树)
- POJ -- 3630 Phone List(Trie 树)
- POJ 3630 Phone List(Trie)
- poj 3630 Phone List (trie~)
- poj 3630 Phone List trie
- poj 3630 Phone List(Trie)
- POJ 3630 Phone List // Trie
- POJ 3630 Phone List 【Trie树入门】
- POJ 3630 Phone List(trie 前缀)
- POJ 3630 Phone List Trie题解
- [poj 3630] Phone List trie 树
- PKU 3630 - Phone List(Trie)
- POJ 3630 Phone List(Trie树,静态数组实现)
- Trie树学习小记 Poj 3630 & Hdu1671 Phone List
- HDU 1671 && POJ 3630 Phone List (Trie树 好题)
- POJ-3630(静态trie树,动态分配空间会TLE)(Phone List)
- HDU 1004 Let the Balloon Rise
- 深度神经网络
- Candy
- linux shell ctrl key function
- C++开源程序库
- POJ 3630 - Phone List (Trie)
- iOS开发ASIHttpRequest
- SwingWorker的使用一
- 字母重排
- 如何异步调用Visual C#函数How to call a Visual C# method asynchronously
- hdu 4849 Wow! Such City!(dijstra)
- 推荐算法之基于物品的协同过滤算法
- 1379: [Baltic2001]Postman
- MDK环境的ARM汇编中内存对齐与对ADR Rd,{PC}+n形式的理解