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

题目链接

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

Pacific Northwest 1998

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

代码如下:

#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;
}
时间: 2024-11-06 15:25:56

poj1056 & hdu1305 & zoj1808 Immediate Decodability(字典树变形)的相关文章

hdu1305Immediate Decodability(字典树)

这题看是否 这题能A是侥幸,解决的办法是先存一下输入的字符串,进行排序. 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 b

POJ 3764 - The xor-longest Path - [DFS+字典树变形]

题目链接:http://poj.org/problem?id=3764 Time Limit: 2000MS Memory Limit: 65536K Description In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edges on p: $_{xor}length(p) = \bigoplus_{e \in p}w(e)$ $\oplus$

HDU 1247 Hat’s Words(字典树变形)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary. You are to find all the hat's words in a dictionary. Input Standa

POJ 1056 IMMEDIATE DECODABILITY (字典树)

IMMEDIATE DECODABILITY Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12972   Accepted: 6222 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 s

Immediate Decodability(字典树)

Immediate Decodability Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2248    Accepted Submission(s): 1168点我 Problem Description An encoding of a set of symbols is said to be immediately decoda

HDU 2846 Repository(字典树变形)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2846 Problem Description When you go shopping, you can search in repository for avalible merchandises by the computers and internet. First you give the search system a name about something, then the syst

/*字典树*/一些简单题

原理很简单,,,,,肯定能看懂,,,我觉得实现费点劲..... 我的模板: #include <iostream> #include<bits/stdc++.h> using namespace std; #define  MAX  26 typedef struct TrieNode { int nCount;  // 该节点前缀 出现的次数 struct TrieNode *next[MAX]; //该节点的后续节点 } TrieNode; TrieNode Memory[10

【暖*墟】 #trie# 字典树的运用

Trie,又称字典树 是一种用于实现字符串快速检索的多叉树结构. 每个节点都拥有若干个字符指针,若在插入或检索字符串时扫描到一个字符c , 就沿着当前节点的c这个字符指针,走向该指针指的节点. 下面我们来详细讨论Trie的基本操作过程. 初始化 一棵空Trie仅包含一个根节点,该点的字符指针均指向空. *********  根节点表示空串.********* 插入 当需要插入一个字符串S时,我们令一个指针P起初指向根节点. 然后,依次扫描S中的每个字符c. 1.若P的c字符指针指向一个已经存在的

字符串hash与字典树

title: 字符串hash与字典树 date: 2018-08-01 22:05:29 tags: acm 算法 字符串 概述 这篇主要是关于字符串里的 字符串hash 和 字符串字典树,,两个都是简单的套模板的东西,,,理解基本思想就行了,,,对了,,还有一个字典树的的变形--01字典树: 字符串hash 如何求一个字符串的hash值 字符串hash的作用就是将 字符串有效的转化为一个整数 ,,这个转化过程利用的是一个 hash函数 例如,,我们选hash函数为 \(hash[i]=(has