Immediate Decodability

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
题意:给定一些字符串,判断是否出现某些字符串是另一些字符串的前缀
题解:按照给定的字符串建立字典树,到字符串末尾时标记字符为1表示建立到结束,否则为0。然后查找如果出现在某个字符串中(没到结束)标记为0的情况
AC代码:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
char ss[1000][1000];
struct lmx{
    int flag;
    struct lmx *a[10];
}*h,*p,*q;
void build(char s[])
{
    p=(lmx*)malloc(sizeof(lmx));
    p=h;
    int i,temp,j;
    for(i=0;i<strlen(s);i++)
    {
        temp=s[i]-'0';
        if(p->a[temp]==NULL)
        {
            q=(lmx*)malloc(sizeof(lmx));
            for(j=0;j<10;j++)
            {
                q->a[j]=NULL;
            }
            q->flag=0;
            p->a[temp]=q;
            p=p->a[temp];
        }
        else
        {
            p=p->a[temp];
        }
    }
    p->flag=1;
}
bool finder(char ss[])
{
    int i,temp;
    p=(lmx*)malloc(sizeof(lmx));
    p=h;
    for(i=0;i<strlen(ss);i++)
    {
        temp=ss[i]-'0';
       p=p->a[temp];
       if(p->flag==1&&i<strlen(ss)-1) {return true;}
    }
    return false;
}
void delet(lmx *p)
{
    int i;
    for(i=0;i<10;i++)
    {
        if(p->a[i]!=NULL)
        {
            delet(p->a[i]);
        }
    }
    delete p;
}
int main()
{
        int i,cnt=0,ca=0,flg,k;
        while(scanf("%s",ss[0])!=EOF)
        {
            ca++;
            h=(lmx*)malloc(sizeof(lmx));
            for(i=0;i<10;i++)
            {
                h->a[i]=NULL;
            }
            h->flag=1;
            build(ss[0]);
            flg=0;
            k=1;
            while(scanf("%s",ss[k]))
            {
                if(strcmp(ss[k],"9")==0) break;
                build(ss[k]);
                k++;
            }
            for(i=0;i<k;i++)
            {
                if(finder(ss[i])) {flg=1;break;}
            }
            printf("Set %d ",ca);
            if(flg==0) puts("is immediately decodable");
            else puts("is not immediately decodable");
           delet(h);
        }
        return 0;
}
posted @ 2013-08-09 20:34  forevermemory  阅读(237)  评论(0编辑  收藏  举报