码迷,mamicode.com
首页 > 其他好文 > 详细

poj1056 & hdu1305 & zoj1808 Immediate Decodability(字典树变形)

时间:2014-09-05 11:29:31      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:hdu   poj   字典树   zoj   

题目链接

poj  1056 :http://poj.org/problem?id=1056

hdu 1305 :http://acm.hdu.edu.cn/showproblem.php?pid=1305

zoj  1808 :http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=808


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

Source



题意:找一堆单词中是否存在一个单词是另一个单词的前缀。


代码如下:

#include <cstdio>
#include <cstring>
#include <malloc.h>
#include <iostream>
using namespace std;
#define MAXN 2
typedef struct Trie
{
    int v;//根据需要变化
    Trie *next[MAXN];
    //next是表示每层有多少种类的数,如果只是小写字母,则26即可,
    //若改为大小写字母,则是52,若再加上数字,则是62了
} Trie;
Trie* root;

void createTrie(char *str)
{
    int len = strlen(str);
    Trie *p = root, *q;
    for(int i = 0; i < len; i++)
    {
        int id = str[i]-'0';
        if(p->next[id] == NULL)
        {
            q = (Trie *)malloc(sizeof(Trie));
            q->v = 1;//初始v==1
            for(int j = 0; j < MAXN; j++)
                q->next[j] = NULL;
            p->next[id] = q;
            p = p->next[id];
        }
        else
        {
            // p->next[id]->v++;
            p = p->next[id];
        }
    }
    p->v = -1;//若为结尾,则将v改成-1表示
}

int findTrie(char *str)
{
    int len = strlen(str);
    Trie *p = root;
    for(int i = 0; i < len; i++)
    {
        int id = str[i]-'0';
        p = p->next[id];
        if(i < len-1)//注意
        {
            if(p->v==-1)
            {
                return -1;
            }
        }
        /* if(p == NULL) //若为空集,表示不存以此为前缀的串
             return 0;
         if(p->v == -1)   //字符集中已有串是此串的前缀
             return -1;*/
    }
    return 1;//自身
}

int main()
{
    char str[15][20];
    int c = 1;
    while(scanf("%s",str[0])!=EOF)
    {
        root = (Trie *)malloc(sizeof(Trie));
        for(int i = 0; i < MAXN; i++)
            root->next[i] = NULL;
        createTrie(str[0]);
        int k = 1;
        while(scanf("%s", str[k])&&str[k][0]!='9')
        {
            createTrie(str[k++]);
        }
        int i;
        for(i = 0; i < k; i++)
        {
            int flag = findTrie(str[i]);
            if(flag == -1)//有相同前缀
            {
                printf("Set %d is not immediately decodable\n",c++);
                break;
            }
        }
        if(i>=k)//没有相同前缀
        {
            printf("Set %d is immediately decodable\n",c++);
        }
    }
    return 0;
}


poj1056 & hdu1305 & zoj1808 Immediate Decodability(字典树变形)

标签:hdu   poj   字典树   zoj   

原文地址:http://blog.csdn.net/u012860063/article/details/39077139

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!