Immediate Decodability(HDU-1305)(字典树的简单应用)

来源:互联网 发布:海岛骑兵医师升级数据 编辑:程序博客网 时间:2024/05/16 15:30

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1305

Immediate Decodability

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3624    Accepted Submission(s): 1891


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
0110001000009011001000009
 
Sample Output
Set 1 is immediately decodableSet 2 is not immediately decodable

题目大意:

这个题输入一些行二进制数,每组之间有一个单独的9隔断,在一组之中要判断任意一个二进制数  是不是 这组其他二进制数的前缀。

如果有一个是前缀则输出 is not immediately decodable,否则输出is immediately decodable

题目分析:

在添加每个二进制数进入字典树时,可以判断添加的过程中,判断是否有两个或者以上的二进制数走过同样的路径,path的值是否大于2,end值是否大于 1。

代码:

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <string>#include <cstdlib>#include <queue>#include <vector>#include <map>using namespace std;typedef long long LL;const int N=10000+999;int flag;struct sky{    int path; //存入二进制数是走过路径的次数    int ch;    int end;  //二进制数结束的存储标记    sky *next[2];    sky(char ch=' ') //初始化    {        this -> ch =ch;        this -> path =this -> end =0;        for(int i=0; i<2; i++)            this -> next[i] =nullptr;    }};sky *root;int add(char *s){    sky *t=root;    while(*s)    {        int i=*s-'0';        if(t->next[i]==nullptr)            t->next[i] = new sky(*s);        t->next[i]->path++;        t=t->next[i];        s++;        if(t->end>0&&t->path>1) //如果在存储的过程中出现end>0则说明这条存储路径上已经存储过二进制数            return 1;           //直接返回    }    t->end++;    if(t->path>1) return 1;     //跟上面作用一样     return 0;}void Delete(sky *x)  //虽然写了释放内存但是这里并没有用到,HDU1671跟这个类似但是需要释放内存。{    for(int i=0; i<10; i++)        if(x->next[i]!=NULL)            Delete(x->next[i]);    delete(x);}int main(){    char num[13];    int cas=1;    flag=0;   //第一组开始前的初始化    root=new sky;    while(scanf(" %s",num)!=EOF)    {        if(num[0]=='9') //这一组结束后输出,继续初始化        {            if(flag==0) printf("Set %d is immediately decodable\n",cas++);            else printf("Set %d is not immediately decodable\n",cas++);            flag=0;            root=new sky;            continue;        }        if(flag) continue;  //如果已经出现前缀则不用处理接下来的二进制数直接跳过        flag=add(num);    }    return 0;}


原创粉丝点击