zoukankan      html  css  js  c++  java
  • 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;
    }
                                                                                      ------江财小子

  • 相关阅读:
    函数地址经典基础C++笔试题(附答案)
    类型事务修改 mysql 表类型 实际测试可执行
    ARM7,ARM9有哪些区别
    I2C,SPI,UART总线的区别详解
    I²C
    AnyWhere——面向设备的编程模式
    Linux设备驱动编程之内存与I/O操作
    http://www.ibm.com/developerworks/cn/linux/lcnspidermonkey/index.html
    PCI Express总线接口板的设计与实现
    armfans文章搜集
  • 原文地址:https://www.cnblogs.com/372465774y/p/2421688.html
Copyright © 2011-2022 走看看