zoukankan      html  css  js  c++  java
  • HDU 1305 Immediate Decodability (字典树)

    Immediate Decodability

    题目链接

    Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 3 Accepted Submission(s) : 2
    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
    Pacific Northwest 1998

    字典树代码如下:

    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    struct node
    {
        int num;
        node *next[2];
    };
    node *newnode()
    {
        node *p = new node ;
        p->num = 0 ;
        p->next[0] = NULL ;
        p->next[1] = NULL ;
        return p ;
    }
    char a[100][100];
    int buildtree(node *root,char *t)
    {
        int l=strlen(t);
        node *p=root;
        for(int i=0;i<l;i++)
        {
            int tr=t[i]-'0';
            if(p->num!=0) return 0;
            if(p->next[tr]==NULL) p->next[tr]= newnode();
            p=p->next[tr];
        }
        if(p->next[0]==NULL&&p->next[1]==NULL&&p->num==0)
            {p->num=1;return 1;}
        else
            return 0;
    
    }
    void delett(node *p)
    {
        if(p->next[0]) delett(p->next[0]);
        if(p->next[1]) delett(p->next[1]);
        delete p ;
    }
    int main()
    {
        int n=0,k=0;
        node *head;
        int m;
        while(scanf("%s",&a[n++])!=EOF)
        {
            if(a[n-1][0]!='9') ;
            else
            {
                int i;
                head=newnode();
                for(i=0;i<n-1;i++)
                {
                    m=buildtree(head,a[i]);
                    if(m==0)
                        break;
                }
                if(i==n-1)
                    printf("Set %d is immediately decodable
    ",++k);
                else
                    printf("Set %d is not immediately decodable
    ",++k);
                n=0;
                delett(head);
            }
        }
        return 0;
    }
    

    用c语言字符串解决代码比较简单但是耗时

    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int main()
    {
        char a[100][100];
        int n;
        int num=0;
        char text[100];
        while(scanf("%s",&text)!=EOF)
        {
            if(strcmp(text,"9")==0)
                break;
            n=1;
            strcpy(a[0],text);
            for(int i=1;scanf("%s",&a[i]);i++)
            {
                if(strcmp(a[i],"9")==0)
                    break;
                n++;
            }
            int k=0,state=0;
            for(int i=0;i<n;i++)
            {
                for(int j=i+1;j<n;j++)
                {
                    if(strstr(a[i],a[j])==&a[i][0]||strstr(a[j],a[i])==&a[j][0])
                        k=1,printf("Set %d is not immediately decodable
    ",++num),state=1;
                }
                if(state==1)
                    break;
            }
            if(!k)
                printf("Set %d is immediately decodable
    ",++num);
        }
        return 0;
    }
    
  • 相关阅读:
    mybatis mapper配置
    python 练习题
    python 函数
    python 文件处理
    python3 编码解码
    messagebox
    Python 基础
    PyMongo
    tkinter Text
    python tkinter entry
  • 原文地址:https://www.cnblogs.com/nanfenggu/p/7900081.html
Copyright © 2011-2022 走看看