字典树
来源:互联网 发布:sql包含某个字符 编辑:程序博客网 时间:2024/06/06 02:00
刷了几道字典树的题目,做做总结。。。
HDU1305
Immediate Decodability
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3779 Accepted Submission(s): 1981Problem DescriptionAn encoding of a set of symbols is said to be immediately decodable if no code for one symbol is the prefix of a code for another symbol. We will assume for this problem that all codes are in binary, that no two codes within a set of codes are the same, that each code has at least one bit and no more than ten bits, and that each set has at least two codes and no more than eight.Examples: Assume an alphabet that has symbols {A, B, C, D}The following code is immediately decodable:A:01 B:10 C:0010 D:0000but this one is not:A:01 B:10 C:010 D:0000 (Note that A is a prefix of C)InputWrite a program that accepts as input a series of groups of records from input. Each record in a group contains a collection of zeroes and ones representing a binary code for a different symbol. Each group is followed by a single separator record containing a single 9; the separator records are not part of the group. Each group is independent of other groups; the codes in one group are not related to codes in any other group (that is, each group is to be processed independently).OutputFor each group, your program should determine whether the codes in that group are immediately decodable, and should print a single output line giving the group number and stating whether the group is, or is not, immediately decodable.Sample Input0110001000009011001000009Sample OutputSet 1 is immediately decodableSet 2 is not immediately decodable
就是判断某个字符串会不会是其它任意字符串的前缀,我是在插入的过程中就进行判断,用flag做了标记,而之前的做法是插入和判断分开,显然前者话费的时间更少。
这也是在做HDU1671时看到了别人的做法学习的,时间优化了不少。贴一下AC的代码。。。
#include <iostream>#include <algorithm>#include <stdlib.h>#include <string.h>#include <stdio.h>#define maxn 10000using namespace std;int flag;struct Tire{ bool end; struct Tire *next[2]; Tire(){ end=false; for(int i=0;i<2;i++) next[i]=NULL; }};Tire *root;void insert(Tire *r,char w[]){ Tire *p=r; for(int i=0;w[i];i++){ int t=w[i]-'0'; if(p->next[t]==NULL){ p->next[t]=new Tire(); } p=p->next[t]; if(p->end) flag=1; } p->end=true;}void clear(Tire *r){ if(r==NULL) return; for(int i=0;i<2;i++) clear(r->next[i]); delete(r);}int main(int argc, const char * argv[]) { char s[maxn]; int count=1; while(scanf("%s",s)!=EOF){ root=new Tire; if(strcmp(s, "9")!=0) insert(root, s); flag=0; while(scanf("%s",s)&&strcmp(s, "9")!=0){ insert(root, s); } if(flag) printf("Set %d is not immediately decodable\n",count); else printf("Set %d is immediately decodable\n",count); count++; } return 0;}
HDU1671
Phone List
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 22181 Accepted Submission(s): 7535Problem DescriptionGiven 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 9112. Alice 97 625 9993. Bob 91 12 54 26In 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.InputThe 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.OutputFor each test case, output “YES” if the list is consistent, or “NO” otherwise.Sample Input2391197625999911254265113123401234401234598346Sample OutputNOYES
#include <iostream>#include <algorithm>#include <stdlib.h>#include <string.h>#include <stdio.h>#define maxn 10000using namespace std;int flag;struct Tire{ bool end; struct Tire *next[2]; Tire(){ end=false; for(int i=0;i<2;i++) next[i]=NULL; }};Tire *root;void insert(Tire *r,char w[]){ Tire *p=r; for(int i=0;w[i];i++){ int t=w[i]-'0'; if(p->next[t]==NULL){ p->next[t]=new Tire(); } p=p->next[t]; if(p->end) flag=1; } p->end=true;}void clear(Tire *r){//释放内存 if(r==NULL) return; for(int i=0;i<2;i++) clear(r->next[i]); delete(r);}int main(int argc, const char * argv[]) { char s[maxn]; int count=1; while(scanf("%s",s)!=EOF){ root=new Tire;//之前少了这句话,在释放内存是会出问题 if(strcmp(s, "9")!=0) insert(root, s); flag=0; while(scanf("%s",s)&&strcmp(s, "9")!=0){ insert(root, s); } if(flag) printf("Set %d is not immediately decodable\n",count); else printf("Set %d is immediately decodable\n",count); count++; } return 0;}
阅读全文
0 0
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树。。
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 数据库第二天
- spring-data-JPA使用JpaRepository注解自定义SQL查询数据库多表查询
- H5——webSocket
- Java案例-用户注册邮箱验证将邮箱激活码存入redis功能实现
- POJ 1976 A Mini Locomotive
- 字典树
- NSData所有API学习
- Triple
- 10.6、spring boot的web应用——@ModelAttribute应用
- 深入解析Java垃圾回收机制
- Monkey使用和APP报名查看
- 求特殊自然数
- 数据结构实验之排序七:选课名单
- AI Challenger全球AI挑战赛落幕,获奖团队分享200万奖金