HDU 1671 Phone List(字典树)
来源:互联网 发布:centos vsftpd 编辑:程序博客网 时间:2024/06/09 23:10
Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 21138 Accepted Submission(s): 7135
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.
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
2391197625999911254265113123401234401234598346
Sample Output
NOYES
Source
2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)
题意:
查询有没有号码是别的号码的前缀。
POINT:
先给号码排个序,短的先入树,这样找的时候只要找包不包含就行了。
因为有两种情况。
911 9111和9111 911。
#include <iostream>#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>using namespace std;#define LL long longconst int N = 101100;int tree[N][10];int sz=1;int vis[N];struct node{ char s[20]; int l;}s[N];void init(){ memset(tree,0,sizeof tree); memset(vis,0,sizeof vis); memset(s,0,sizeof s); sz=1;}void build(char ss[],int &flag){ int u=0; int l=strlen(ss); for(int i=0;i<l;i++) { int j=ss[i]-'0'; if(tree[u][j]==0) { tree[u][j]=sz++; } u=tree[u][j]; if(vis[u]==2) { flag=1; return; } } vis[u]=2; }bool cmd(node a,node b){ return a.l>b.l;}int main(){ int T; scanf("%d",&T); while(T--) { init(); int n;scanf("%d",&n); int flag=0; for(int i=1;i<=n;i++) { scanf("%s",s[i].s); s[i].l=strlen(s[i].s); } sort(s+1,s+1+n,cmd); for(int i=n;i>=1;i--) { if(flag) break; build(s[i].s,flag); } if(flag) printf("NO\n"); else printf("YES\n"); }}
阅读全文
0 0
- hdu 1671 Phone List(字典树)
- HDU-#1671 Phone List(字典树)
- hdu 1671 phone list(字典树)
- HDU 1671 Phone List(字典树)
- HDU 1671 Phone List(字典树)
- HDU 1671 Phone List(字典树)
- HDU 1671 Phone List(字典树)
- HDU 1671 Phone List (字典树)
- HDU 1671 Phone List(字典树)
- hdu 1671 Phone List(字典树)
- HDU 1671 Phone List (字典树)
- HDU 1671 Phone List(字典树)
- hdu 1671 Phone List(字典树)
- HDU 1671 Phone List(字典树)
- hdu 1671 Phone List 字典树
- [字典树 ] hdu 1671 phone list
- 【字典树】hdu 1671 Phone List
- Hdu 1671 Phone List 字典树
- 基于JS的AB测试方案
- at命令
- Python打包分发工具setuptools简介
- Intervals (差分约束)
- How Many Tables(HDU
- HDU 1671 Phone List(字典树)
- 友盟 分享
- 算法基础-字符移位
- LocationManager的使用
- Java中的static关键字解析
- mysql基本使用
- 如何解决斐波那契数列递归方法的性能问题
- 淘宝框架atlas集成
- 为什么你该开始学习编程了?