POJ 3630 Phone List 静态字典树
2015-10-20 21:35
585 查看
Phone List
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 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 Nordic 2007 |
[Discuss]
![](http://poj.org/images/home.gif)
Home Page
![](http://poj.org/images/goback.gif)
Go
Back
![](http://poj.org/images/top.gif)
To top
要静态树动态树会超时建树前要sort不然这组数据会错
2 2 1 12 2 12 1 NO NO
ACcode:
#pragma warning(disable:4786)//使命名长度不受限制 #pragma comment(linker, "/STACK:102400000,102400000")//手工开栈 #include <map> #include <set> #include <queue> #include <cmath> #include <stack> #include <cctype> #include <cstdio> #include <cstring> #include <stdlib.h> #include <iostream> #include <algorithm> #define rd(x) scanf("%d",&x) #define rd2(x,y) scanf("%d%d",&x,&y) #define rds(x) scanf("%s",x) #define rdc(x) scanf("%c",&x) #define ll long long int #define maxn 100005 #define mod 1000000007 #define INF 0x3f3f3f3f //int 最大值 #define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i) #define MT(x,i) memset(x,i,sizeof(x)) #define PI acos(-1.0) #define E exp(1) #define MAX 10 using namespace std; struct Trie{ Trie *next[MAX]; int v; Trie(){ FOR(i,0,MAX-1) next[i]=NULL; v=1; } }node[maxn]; struct N{ char str[50]; int len; friend bool operator<(N a,N b){ return a.len<b.len; } }my[maxn]; Trie *root; int cnt; void createTrie(char *str){ int len=strlen(str); Trie *p=root,*q; FOR(i,0,len-1){ int id=str[i]-'0'; if(p->next[id]==NULL){ q=&node[cnt++]; p->next[id]=q; p=p->next[id]; } else{ p->next[id]->v++; p=p->next[id]; } } p->v=111; } int find_Trie(char *str){ int len=strlen(str); Trie *p=root; FOR(i,0,len-1){ int id=str[i]-'0'; if(p->next[id]==NULL) break; p=p->next[id]; } return (p->v==111?1:0); } int n; int main(){ int loop; rd(loop); while(loop--){ rd(n); cnt=0; MT(node,0); root=&node[cnt++]; FOR(i,0,n-1){ rds(my[i].str); my[i].len=strlen(my[i].str); } bool flag=true; sort(my,my+n); FOR(i,0,n-1) createTrie(my[i].str); FOR(i,0,n-1){ flag=find_Trie(my[i].str); if(!flag)break; } printf(flag?"YES\n":"NO\n"); //deal(root); } return 0; } /* 2 3 911 976259993 9112546 5 113 12340 123440 12345 98346 */
相关文章推荐
- ACE的线程管理机制
- poj Popular Cows 2186 (强连通分量 求有多少个点可以被 其他所有点到达) 好题
- 决策树1---特征选择(信息增益)
- 应用程序框架实战十三:DDD分层架构之我见(转)
- java环境配置和tomcat环境配置
- DOM对象
- 基于ACE的后台服务程序的实现
- js小妙用
- 十六进制颜色码
- 深入分析面向对象中的对象概念(转)
- 面试题38:数字在排序数组中出现的次数
- 之前所学的整理(截止到面向对象编程)
- uc浏览器qq浏览器广告过滤
- 你不知道的JavaScript--Item12 undefined 与 null
- 第一款采用人工神经网络的智能输入法
- Hadoop2.0(HDFS2)以及YARN设计的亮点
- 地址
- C#中ToString()格式详解
- C++11 原始字符串
- 你不知道的JavaScript--Item12 undefined 与 null