zoukankan      html  css  js  c++  java
  • UVA644

     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

    题目大意:输入几行二进制数,以输入9为结束,表示输完一组。
    在这一组数上,如果一个数不是其他任何一个数的前缀的话,就输出Set 1 is immediately decodable。否则输出Set 2 is not immediately decodable。
    思路:使用了strstr函数。strstr(str1,str2):该函数返回str2第一次在str1中的位置,如果没有找到,返回NULL。
    charstr[]="1234xyz";
    char*str1=strstr(str,"34");
    cout<<str1<<endl;                                    
    结果为:34xyz
     
    #include<cstdio>
    #include<string.h>
    #include<stdlib.h>
    
    char s[15][15];
    
    int cmp(const void*_a,const void*_b)//字符串按字典序排序
    {
        char * a=(char *)_a;
        char * b=(char *)_b;
        return strcmp(a,b);
    }
    
    int main()
    {
        int i,j,k=1,n,flag;
        while(gets(s[0]) != 0)
        {
            n=0;
            char *p=NULL;//定义一个空指针
            while(1)
            {
                if(strcmp(s[n],"9") == 0)
                    break;
                n++;
                gets(s[n]);
            }
            qsort(s,n,sizeof(s[0]),cmp);
            flag=1;
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                {
                    if(i!=j)
                    {
                        p=strstr(s[i],s[j]);   //一个非常好的函数
                        //不止p!=NULL,还要 (strcmp(p,s[i])==0)。
                        //因为是前缀不能相同,所以要从s[0]就相同,返回的自然就与s[i]相同了。
                        if(p!=NULL && (strcmp(p,s[i])==0))  
                        {
                            flag=0;
                            break;
                        }
                    }
                }
                if(flag==0)
                    break;
            }
            if(flag==0)
                printf("Set %d is not immediately decodable
    ",k++);
            else
                printf("Set %d is immediately decodable
    ",k++);
        }
        return 0;
    }
  • 相关阅读:
    上班5个月总结
    使用余弦定理计算两篇文章的相似性
    9月10日 小结
    软件测试
    《增长黑客》笔记
    统计学术语
    数据分析师:数据分析工作常见七种错误及其规避技巧(转自经管之家)
    输入一个日期,计算这个日期与 2018-03-12差多少天;
    求输入数字的阶乘 及加和 #s=1!+2!+3!+…..+n!
    列表去重
  • 原文地址:https://www.cnblogs.com/youdiankun/p/3690097.html
Copyright © 2011-2022 走看看