• hdu 1305 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)
     


     

    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

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define N 3333
    typedef struct node
    {
        int c;
        int flag;
    }BT;
    BT tr[N];
    int f=0;
    void ct(int i)  //建树,可以成为01树噢,左孩子为0,右孩子为1
    {
        tr[i<<1].c='0';
        tr[i<<1].flag=0;
        tr[i<<1|1].c='1';
        tr[i<<1|1].flag=0;
       if(i*2+1<1024)
       {
           ct(i<<1);
           ct(i<<1|1);
       }
    }
    void pan(char *s,int i,int k)//大概思路是将输入的编码放进树里,路上如果遇见了已标记的,说明有前缀
    {
            if(f)
           return;
        if(tr[k<<1].c==s[i])
           {
               if(tr[k<<1].flag==1)
                 { f=1;return; }
            if(s[i+1]=='\0')
                {tr[k<<1].flag=1;return ;}
               pan(s,i+1,k<<1);

           }
           else
             {
                 if(tr[k<<1|1].flag==1)
                   {f=1;return;}
                   if(s[i+1]=='\0')
                {tr[k<<1|1].flag=1;return ;}
                 pan(s,i+1,k<<1|1);
             }
    }
    int main()
    {
      freopen("in.txt","r",stdin);
      int t=1;
      char s[13];
        ct(1);//建树
      while(scanf("%s",s)!=EOF)
      {
          if(strcmp(s,"9")==0)
            {
              if(!f)
                  printf("Set %d is immediately decodable\n",t++);
                else
                  printf("Set %d is not immediately decodable\n",t++);
               f=0;
               ct(1);//建树
               continue;
            }
        pan(s,0,1);将编码放进树中,并在结尾标记!
      }

      return 0;
    }
                                                                                      ------江财小子

  • 相关阅读:
    CoreJava逻辑思维-顺时针打印自定义矩阵
    微信公众号开发引言
    .Net自动更新程序GeneralUpdate,适用于wpf,winfrom,控制台应用
    .net技术栈转型心路历程分享
    TCP/IP网络编程之数据包协议
    TCP/IP网络编程之字节序和网络字节序
    TCP/IP网络编程之socket交互流程
    Linux入门笔记
    WPF新手快速入门系列 3.MVVM
    WPF新手快速入门系列 2.绑定
  • 原文地址:https://www.cnblogs.com/372465774y/p/2421688.html
Copyright © 2020-2023  润新知