• POJ


    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 standard 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

    题意:输入几组由1和0组成的字符串,以输入9为输入终止标志。判断输入的所有字符串中,是否有重复的前缀。如样例而中01与010前缀重复,因此输出否定。

    在建树时即可根据已建部分check去重,首先有两种情况考虑,一种是先遇到了重复前缀中较短的,那么一旦有重复走过的其他单词的结尾,说明该单词有与其他单词重复的前缀。ans标记为false
    另一种是,先遇到了较长串,较短串重复了,那么在较短串建树插入完成后,发现其最终结尾节点小于最大节点标号,那么说明其整个串都是走过,存在过的节点,整个串都和一个较长串的前缀重复。ans标记为false

    剩下的就直接根据ans标记输出答案即可
    代码如下:

    #include<stdio.h>///字典树建树同时判断重复前缀
    #include<string.h>
    int tot,tre[100][3];
    bool vis[100],ans;
    int insert(char str[],int rt)
    {
        int len=strlen(str);
        for(int i=0; i<len; i++)
        {
            int x=str[i]-'0';
            if(tre[rt][x]==0)
            {
                tre[rt][x]=++tot;
                memset(tre[tre[rt][x]],0,sizeof(tre[tre[rt][x]]));
            }
            rt=tre[rt][x];
            if(vis[rt])ans=false;
        }
        if(rt<tot)ans=false;
        vis[rt]=true;
    }
    int main()
    {
        char str[100];
        tot=0;
        int num=++tot,cnt=0;
        memset(vis,false,sizeof(vis));
        memset(tre[num],0,sizeof(tre[num]));
        ans=true;
        while(scanf("%s",str)!=EOF)
        {
            if(str[0]!='9') insert(str,num);
            else
            {
                printf("Set %d %s
    ",++cnt,ans?"is immediately decodable":"is not immediately decodable");
                tot=0;
                int num=++tot,cnt=0;
                memset(vis,false,sizeof(vis));
                memset(tre[num],0,sizeof(tre[num]));
                ans=true;
            }
        }
    }
    
  • 相关阅读:
    zzuli2470: 迷宫
    zzuli2460: 楼上真的是签到题
    zzuli2460: 楼上真的是签到题
    洛谷P1044 :栈(卡特兰数)
    洛谷P1044 :栈(卡特兰数)
    洛谷P1056:排座椅(贪心)
    代码块地址
    tabBarItem动画
    vim Podfile
    webView进度条
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11135864.html
Copyright © 2020-2023  润新知