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

#include <iostream>

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

using namespace std;

typedef struct Node

{

struct Node *next[2];

int flag;

}Node,*Tree;

int flag1;

void Creat(Tree &T)

{

T=(Node *)malloc(sizeof(Node));

T->flag=0;

for(int i=0;i<2;i++)

T->next[i]=NULL;

}

void insert(Tree &T,char *s)

{

Tree p=T;

int t;

int l=strlen(s);

for(int i=0;i<l;i++)

{

t=s[i]-‘0‘;

if(p->next[t]==NULL)

Creat(p->next[t]);

p=p->next[t];

if(p->flag>0)

flag1=0;

}

p->flag++;

}

void D(Tree p)

{

for(int i=0;i<2;i++)

{

if(p->next[i]!=NULL)

D(p->next[i]);

}

free(p);

}

int main()

{

char a[30];

int kk=0;

Tree T;

while(scanf("%s%*c",a)!=EOF)

{

Creat(T);

kk++;

flag1=1;

insert(T,a);

while(scanf("%s",a)!=EOF)

{

if(strcmp(a,"9")==0)

break;

insert(T,a);

}

if(flag1)

printf("Set %d is immediately decodable\n",kk);

else printf("Set %d is not immediately decodable\n",kk);

D(T);

}

return 0;

}

hdu1305Immediate Decodability(字典树),布布扣,bubuko.com

时间: 2024-09-09 07:34:58

hdu1305Immediate Decodability(字典树)的相关文章

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

poj1056 &amp; hdu1305 &amp; 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 de

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

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

hdu 1251 统计难题(字典树)

Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注意:本题只有一组测试数据,处理到文件结束. Output 对于每个提

51nod round3# 序列分解(折半枚举+字典树)

小刀和大刀是双胞胎兄弟.今天他们玩一个有意思的游戏. 大刀给小刀准备了一个长度为n的整数序列.小刀试着把这个序列分解成两个长度为n/2的子序列. 这两个子序列必须满足以下两个条件: 1.他们不能相互重叠. 2.他们要完全一样. 如果小刀可以分解成功,大刀会给小刀一些糖果. 然而这个问题对于小刀来说太难了.他想请你来帮忙. Input 第一行给出一个T,表示T组数据.(1<=T<=5) 接下来每一组数据,输入共2行. 第一行包含一个整数n (2<=n<=40且为偶数). 第二行给出n

白话算法与数据结构之【字典树】

1. 什么是trie树 1.Trie树 (特例结构树) Trie树,又称单词查找树.字典树,是一种树形结构,是一种哈希树的变种,是一种用于快速检索的多叉树结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高.      Trie的核心思想是空间换时间.利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的. Trie树也有它的缺点,Trie树的内存消耗非常大.当然,或许用左儿子

[算法系列之二十]字典树(Trie)

一 概述 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计. 二 优点 利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希表高. 三 性质 (1)根节点不包含字符,除根节点外每一个节点都只包含一个字符: (2)从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串: (3)每个节点的所有子节点包含的字符都不相同. 单词列表为"apps&

poj 3764 The xor-longest Path(字典树)

题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值,找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每个节点到根节点路径的亦或和rec,那么任意路径均可以表示rec[a] ^ rec[b],所以问题 就转换成在一些数中选出两个数亦或和最大,那么就建立字典树查询即可. #include <cstdio> #include <cstring> #include <algorithm> us