首页 > ACM题库 > HDU-杭电 > HDU 1305 Immediate Decodability-字典树-[解题报告] C++
2013
12-04

HDU 1305 Immediate Decodability-字典树-[解题报告] C++

Immediate Decodability

问题描述 :

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)

输入:

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).

输出:

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.

样例输入:

01
10
0010
0000
9
01
10
010
0000
9

样例输出:

Set 1 is immediately decodable
Set 2 is not immediately decodable

题意:在多个字符串里寻找,若他们至少有一个串是另一个串的前缀,则按题目要求输出语气

思路:建立字典树,因为只有0 1两数,数组开2大就行,判断是前缀的方法:每次插入一个字符串,在字典树中查询,若当到达已经存在了的字符串,即cur->cnt==1 ,也就是插入字符串的前缀,或者在字典树中能够找到插入的字符串

代码:

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <cstdlib>
#define lson l,mid,num<<1
#define rson mid+1,r,num<<1|1
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;
        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;
}


  1. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }