HDU 1305 Immediate Decodability (字典树)
来源:互联网 发布:淘宝卖家售假 编辑:程序博客网 时间:2024/06/05 17:13
Immediate Decodability
题目链接
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 2
Problem Description
An 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:0000
but this one is not:
A:01 B:10 C:010 D:0000 (Note that A is a prefix of C)
Input
Write 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).
Output
For 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 Input
01
10
0010
0000
9
01
10
010
0000
9
Sample Output
Set 1 is immediately decodable
Set 2 is not immediately decodable
Source
Pacific Northwest 1998
字典树代码如下:
#include<algorithm>#include<cstdio>#include<cstring>using namespace std;struct node{ int num; node *next[2];};node *newnode(){ node *p = new node ; p->num = 0 ; p->next[0] = NULL ; p->next[1] = NULL ; return p ;}char a[100][100];int buildtree(node *root,char *t){ int l=strlen(t); node *p=root; for(int i=0;i<l;i++) { int tr=t[i]-'0'; if(p->num!=0) return 0; if(p->next[tr]==NULL) p->next[tr]= newnode(); p=p->next[tr]; } if(p->next[0]==NULL&&p->next[1]==NULL&&p->num==0) {p->num=1;return 1;} else return 0;}void delett(node *p){ if(p->next[0]) delett(p->next[0]); if(p->next[1]) delett(p->next[1]); delete p ;}int main(){ int n=0,k=0; node *head; int m; while(scanf("%s",&a[n++])!=EOF) { if(a[n-1][0]!='9') ; else { int i; head=newnode(); for(i=0;i<n-1;i++) { m=buildtree(head,a[i]); if(m==0) break; } if(i==n-1) printf("Set %d is immediately decodable\n",++k); else printf("Set %d is not immediately decodable\n",++k); n=0; delett(head); } } return 0;}
用c语言字符串解决代码比较简单但是耗时
#include<algorithm>#include<cstdio>#include<cstring>using namespace std;int main(){ char a[100][100]; int n; int num=0; char text[100]; while(scanf("%s",&text)!=EOF) { if(strcmp(text,"9")==0) break; n=1; strcpy(a[0],text); for(int i=1;scanf("%s",&a[i]);i++) { if(strcmp(a[i],"9")==0) break; n++; } int k=0,state=0; for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if(strstr(a[i],a[j])==&a[i][0]||strstr(a[j],a[i])==&a[j][0]) k=1,printf("Set %d is not immediately decodable\n",++num),state=1; } if(state==1) break; } if(!k) printf("Set %d is immediately decodable\n",++num); } return 0;}
- HDU 1305 Immediate Decodability (字典树)
- hdu 1305 Immediate Decodability(字典树)
- Hdu 1305 Immediate Decodability 字典树
- hdu 1305 Immediate Decodability (字典树)
- hdu 1305 Immediate Decodability 字典树
- hdu 1305 Immediate Decodability 简单字典树
- hdu 1305 Immediate Decodability E字典树
- hdu 1305-Immediate Decodability(字典树)
- 【hdu】 Immediate Decodability (字典树)
- hdu 1305 Immediate Decodability (字典树入门)
- 1305 Immediate Decodability(字典树)
- IMMEDIATE DECODABILITY(字典树)
- HDU 1305 Immediate Decodability HDU 1671 Phone List(字典树)
- (step5.1.2)hdu 1305(Immediate Decodability——字典树)
- HDU 1305 Immediate Decodability 简单字典树 题意
- HDU-1305 Immediate Decodability (Trie 字典树 入门题)
- hdu 1035 Immediate Decodability(字典树)
- hdu 1305 Immediate Decodability(Trie树)
- Linux下U盘无法挂载以及文件夹出错
- poj-1287-Networking-最小生成树
- Java学习之旅--线程的创建方法
- java窗体Swing效果体验(并没有什么卵用)
- sync同步指令
- HDU 1305 Immediate Decodability (字典树)
- Android Studio 与git 添加忽略文件
- 认识BeanPostProcessor接口
- POJ3528 (三维凸包模板)
- marquee存在两个的时候如何消除自带边距
- js的常用函数
- 设计模式(20)--状态模式
- WdatePicker时间控件设置日期栏中时分秒的初始值
- mjpg streamer记录