hdu1305-Immediate Decodability 字典树

Immediate Decodability

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


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

题目大意给你一些二进制组成的密码,询问是否可以解码,规定如果这些密码里存在某些二进制密码是另外一些二进制密码的前缀,则不可以解码,如果不是则代表可以解码,以数字9代表当前一组数据输入结束。

解题思路没得到一组密码,就将该密码插入到字典树里面去,因为这里只有0和1,所以密码树只有两个子树1和0,当存在情况为当前密码存储的位置被标记过时,则说明该位置存在密码,即存在前缀情况,结束。

ac代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int M=100005;
struct node
{
    int cnt;
    node *child[2];
    node()
    {
        cnt=0;
        for(int i=0; i<2; i++)
            child[i]=NULL;
    }
};
char a[15];
int flag;
node *root,*cur,*newnode;
void insert(char *a)
{
    cur=root;
    int len=strlen(a);
    for(int i=0; i<len ; i++)
    {
        int index=a[i]-'0';
        if(cur->child[index]!=NULL)
        {
            cur=cur->child[index];
            if(cur->cnt==1 || i==len-1)
            {
                flag=0;//标记 
                break;
            }
        }
        else
        {
            newnode=new node;
            cur->child[index]=newnode;
            cur=newnode;
        }
    }
    cur->cnt=1;
}
void del(node *head)
{
    for(int i=0; i<2; i++)
        if(head->child[i]!=NULL)
            del(head->child[i]);
    delete(head);
}
int main()
{
    int g=1;
    while(scanf("%s",a)!=EOF)
    {
        flag=1;//标记默认为1 
        root=new node;
        insert(a);
        while(scanf("%s",a)!=EOF)
        {
            if(strcmp(a,"9")==0)break;
            {
                if(!flag)
                    continue;
                insert(a);
            }
        }
        if(flag)printf("Set %d is immediately decodable\n",g++);
        else
            printf("Set %d is not immediately decodable\n",g++);
        del(root);
    }
    return 0;
}


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


有关字典树的有关知识请看我的字典树专题博客:点击打开链接http://blog.youkuaiyun.com/wang_heng199/article/details/76448804


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值