hdu1305(字典树)
来源:互联网 发布:500万双色球过滤软件 编辑:程序博客网 时间:2024/06/05 14:34
Immediate Decodability
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1513 Accepted Submission(s): 766
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)
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
0110001000009011001000009
Sample Output
Set 1 is immediately decodableSet 2 is not immediately decodable
Source
Pacific Northwest 1998
Recommend
Eddy
本题给定一系列的01序列,要问是否有某个是另一个的前缀。
1.字典树解法
数据较小,可以直接用字典树过掉。字典树结构体中增加是否为字符串末尾的标志位。
#include<iostream>#include<cstdio>#include<cstring>using namespace std;char str[20];bool tag;struct node{node *next[2];bool flag;node(){ next[0]=next[1]=NULL;flag=false;}};void insert(node *root,char *s){int i=0,len=strlen(s);node *p=root;while(s[i]){int id=s[i]-'0';if(p->next[id]){p=p->next[id];if(p->flag)tag=false;}else {p->next[id]=new node;p=p->next[id];}i++;}p->flag=true;}void del(node *root){if(root==NULL)return ;del(root->next[0]);del(root->next[1]);delete root;}int main(){int cas=0;while(~scanf("%s",str)){node *Root=new node;tag=true;do{insert(Root,str);scanf("%s",str);}while(strcmp(str,"9"));del(Root);if(tag)printf("Set %d is immediately decodable\n",++cas);else printf("Set %d is not immediately decodable\n",++cas);}return 0;}
2.二叉树解法
用数组模拟二叉树,0往左走,1往右走,每串结束位置做下标记。
#include<iostream>#include<cstdio>#include<cstring>using namespace std;char str[20];bool tag;int BaniTree[20*10];void insert(char *s){int i=0,id=1;while(s[i]){if(s[i]=='0')id=id<<1;else id=(id<<1)|1;if(BaniTree[id])tag=false;i++;}BaniTree[id]=1;}int main(){int cas=0;while(~scanf("%s",str)){memset(BaniTree,0,sizeof(BaniTree));tag=true;do{insert(str);scanf("%s",str);}while(strcmp(str,"9"));if(tag)printf("Set %d is immediately decodable\n",++cas);else printf("Set %d is not immediately decodable\n",++cas);}return 0;}
- hdu1305字典树
- hdu1305之字典树
- hdu1305(字典树)
- hdu1305(字典树)
- hdu1305 Immediate Decodability (字典树)
- hdu1305-Immediate Decodability 字典树
- hdu1305-Immediate Decodability 字典树
- hdu1305 Immediate Decodability(字典树)
- hdu1305 字典树水题
- poj1056 & hdu1305 & zoj1808 Immediate Decodability(字典树变形)
- HDU1305(二叉树)
- ZOJ1808 POJ1056 HDU1305 Immediate Decodability,Trie树
- Immediate Decodability(hdu1305)
- HDU1305 Immediate Decodability
- hdu1305 Immediate Decodability
- HDU1305——Immediate Decodability
- 字典树
- 字典树
- 码农如何快速打造一个有设计感的网站
- 数据库常见面试题总结
- 数组一
- 游戏编程书籍
- linux命令
- hdu1305(字典树)
- linux常用命令
- sublime 快捷键
- 微软正在为Windows移动系统研究一款“Siri”
- 利用泛型避免工厂方法强制类型转换
- mac下修改hosts
- ubuntu下Android SDK中adb找不到的解决方案
- poj 1836 Alignment
- 网易面试题(社招)