首页 > 其他分享 >UVA Immediate Decodability(简单字典树)

UVA Immediate Decodability(简单字典树)

时间:2023-04-20 22:35:41浏览次数:38  
标签:node codes group struct immediately int Immediate Decodability UVA


Immediate Decodability


Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu


Submit  Status


Description



UVA       Immediate Decodability(简单字典树)_#include



  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 a data file. 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.


The Sample Input describes the examples above.

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





Miguel A. Revilla
2000-01-17






#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

char a[1000200][100];

struct node
{
    int flag;
    struct node *next[10];
};

int ff;

struct node *make()
{
    struct node *p = (struct node*)malloc(sizeof(struct node));
    for(int i=0;i<10;i++)
    {
        p->next[i] = NULL;
    }
    p->flag = 0;
    return p;
}

void charu(struct node *root,char s[])
{
    int k = strlen(s);
    int t;
    struct node *p = root;
    for(int i=0;i<k;i++)
    {
        if(s[i]>='0' && s[i]<='9')
        {
            t = s[i] - '0';
        }
        if(p->flag == 1)
        {
            ff = 1;
        }
        if(p->next[t] == NULL)
        {
            p->next[t] = make();
        }
        p = p->next[t];
    }
    if(p->flag == 1)
    {
        ff = 1;
        return ;
    }
    for(int i=0;i<10;i++)
    {
        if(p->next[i]!=NULL)
        {
            ff = 1;
            break;
        }
    }
    p->flag = 1;
    return ;
}

int main()
{
    int pt = 0;
    while(scanf("%s",&a[0])!=EOF)
    {
         int n = 1;
         ff = 0;
         if(a[0][0] =='9')
         {
             continue;
         }
         struct node *p;
         p = make();

         while(scanf("%s",a[n])!=EOF)
         {
             if(a[n][0] == '9')
             {
                 break;
             }
             n++;
         }
         for(int i=0;i<=n;i++)
         {
             charu(p,a[i]);
             if(ff == 1)
             {
                 break;
             }
         }
         if(ff == 0)
         {
             printf("Set %d is immediately decodable\n",++pt);
         }
         else
         {
             printf("Set %d is not immediately decodable\n",++pt);
         }

    }
    return 0;
}





标签:node,codes,group,struct,immediately,int,Immediate,Decodability,UVA
From: https://blog.51cto.com/u_14834528/6210818

相关文章

  • UVA10237 Bishops
      #include<iostream>#include<cstring>#include<queue>usingnamespacestd;constintN=2e5+2;#defineintlonglongintn,m,f1[50][2000],f2[50][2000];voidsov(){ memset(f1,0,sizeoff1);memset(f2,0,sizeoff2); f1[0][0]=f2......
  • UVA How Many Points of Intersection?
      HowManyPointsofIntersection? a dotsonthetoprowand b dotsonthebottomrow.Wedrawlinesegmentsconnectingeverydotonthetoprowwitheverydotonthebottomrow.Thedotsarearrangedinsuchawaythatthenumberofinternalintersectio......
  • (UVA) The ? 1 ? 2 ? ... ? n = k problem
    The?1?2?...?n=kproblemTheproblemGiventhefollowingformula,onecansetoperators'+'or'-'insteadofeach'?',inordertoobtainagivenk?1?2?...?n=kForexample:toobtaink=12,theexp......
  • How Many O's? UVA - 11038
    写下区间[a,b]的所有数 ,问一共有多少个0 #include<iostream>#include<cstring>#include<vector>usingnamespacestd;#defineintlonglongintn,f[40][40][2][2];vector<int>a;intdfs(intx,intcnt0,intflg,intlead){ if(x<0){ i......
  • Investigating Div-Sum Property UVA - 11361
     定问在[A,B]中,有多少个整数本身能被m整除,各个数位上数字之和也能被m整除?  #include<iostream>#include<cstring>#include<vector>usingnamespacestd;vector<int>a;intm,f[40][105][105][2];intdfs(intx,intv1,intv2,intflg){ if(x<0) retur......
  • UVA11806 Cheerleaders
    你有一个n×m的网格图,现在你要将K个人放在网格中,满足一下条件:网格图的四个边都至少有一个人。每个格子上不能有两个人。每个人必须都有位置。注意:四个角的人可以同时算作在两个边上  容斥原理   J=0时就是allAnswer#include<iostream>#include<cstri......
  • Hackers' Crackdown UVA11825
    你需要将n个集合分成尽量多组,使得每一组里面所有集合的并集等于全集  32122022014111013120   f[S]=max(f[S],f[S-j]+1)且j是一个包含所有点的集合#include<iostream>#include<algorithm>#include<cstring>usingname......
  • Robotruck UVA - 1169
    有n个垃圾,第i个垃圾的坐标为(xi,yi),重量为wi。有一个机器人,要按照编号从小到大的顺序捡起所有垃圾并扔进垃圾桶(垃圾桶在原点(0,0))。机器人可以捡起几个垃圾以后一起扔掉,但任何时候其手中的垃圾总重量不能超过最大载重C。两点间的行走距离为曼哈顿距离(即横坐标之差的绝对值加上纵......
  • Add Again UVA - 11076
     defineS,itissumofallpossiblepermutationsofagivensetofdigits.Forexample,ifthedigitsare<123>,thensixpossiblepermutationsare<123>,<132>,<213>,<231>,<312>,<321>andthesumofthemis......
  • The Super Powers UVA - 11752
     求1~2^64区间里,有多少合法数X合法数:X=a^b,至少存在2个不同的a #include<iostream>#include<algorithm>#include<vector>usingnamespacestd;constintN=65536+3;intb[int(1e6)];__int128_tMAX=1;voidinit(){ inti,j; b[0]=b[1]=1; fo......