hdu----(1671)Phone List(Trie带标签)
Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10837 Accepted Submission(s): 3735
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
2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)
简单字典树:
代码: 实现 insert ,query,delete 三部分
1 /*hdu 1671 ×ÖµäÊ÷*/
2 #define LOCAL
3 #include<cstdio>
4 #include<cstring>
5 #include<iostream>
6 using namespace std;
7 struct Trie
8 {
9 struct Trie *next[10];
10 bool tail;
11 };
12 char str[10005][12];
13 void in_Trie(char *s,Trie *root)
14 {
15 Trie *cur=root,*newcur;
16 for(int i=0;s[i]!=' ';i++)
17 {
18 if(cur->next[s[i]-'0']==NULL)
19 {
20 newcur=new Trie; //(Trie*)malloc(sizeof(sizeof(Trie)));
21 for(int j=0;j<=9;j++)
22 newcur->next[j]=NULL;
23 newcur->tail=0;
24 cur->next[s[i]-'0']=newcur;
25 }
26 cur=cur->next[s[i]-'0'];
27 }
28 cur->tail=1;
29 }
30 bool query(char *s,Trie *root)
31 {
32 int i=0;
33 Trie *cur=root;
34 for(i=0;s[i];i++){
35 if(cur->tail==0&&cur->next[s[i]-'0']!=NULL)
36 cur=cur->next[s[i]-'0'];
37 else break;
38 }
39 if(s[i]!=' ') return 1;
40 return 0;
41 }
42 void dele(Trie *root)
43 {
44 for(int i=0 ; i<=9 ; i++ )
45 if(root->next[i]!=NULL)
46 dele(root->next[i]);
47 // free(root);
48 delete root;
49 }
50 int main()
51 {
52 #ifdef LOCAL
53 freopen("test.in","r",stdin);
54 #endif
55 int t,i,n;
56 Trie *root;
57 scanf("%d",&t);
58 while(t--)
59 {
60 root = new Trie ;
61 for(int j=0;j<=9;j++)
62 root->next[j]=NULL;
63 root->tail=0;
64
65 scanf("%d",&n);
66 for(i=0;i<n;i++){
67 scanf("%s",str[i]);
68 in_Trie(str[i],root);
69 }
70
71 for(i=0;i<n;i++)
72 if(query(str[i],root)!=0)
73 break;
74 dele(root);
75 if(i>=n)
76 printf("YESn");
77 else
78 printf("NOn");
79 }
80 return 0;
81 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Spring中异步注解@Async的使用、原理及使用时可能导致的问题
- Elasticsearch 聚合数据结果不精确,怎么破?
- Elasticsearch 预处理没有奇技淫巧,请先用好这一招!
- 时滞微分方程的matlab解法
- Zabbix Monitoring Logstash
- Join()和 List()的 6 种化学反应
- lettuce和hbase中对netty的使用你都了解吗?
- SAS-新规试行下诞生的程序
- AIOHTTP的实战(一)
- JMeter二次开发插件实例
- 「R」Obtain RNAseq Values for a Specific Gene in Xena Database
- 使用JMeter测试TCP协议
- SQL使用(一):如何使用SQL语句去查询第二高的值
- OpenCV加载图片显示对应类型(位深度)方法
- 小程序快速入门教程(登录注册、开发工具、文件及结构介绍)