题目地址:http://poj.org/problem?id=1056

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



将各个编码序列作为二叉树的节点序列建立二叉树,并在过程中标记编码序列的最后一位,然后遍历二叉树,如果存在非叶节点的istail为1,即可说明存在某序列为另一序列的前缀序列。

#include <stdio.h>
#include <stdlib.h> typedef struct btree{
int istail;
struct btree * left;
struct btree * right;
}BTree, *pBTree; char data[11];
BTree * root = NULL; void insert(char data[]){
int i= 0;
BTree * p = NULL;
if (root == NULL){
root = (BTree *)malloc(sizeof(BTree));
root->istail = 0;
root->left = root->right = NULL;
}
p = root;
while (data[i] != '\0'){
if (data[i] == '0'){
if (p->left != NULL){
p = p->left;
} else{
p->left = (BTree *)malloc(sizeof(BTree));
p = p->left;
p->istail = 0;
p->left = p->right = NULL;
}
} else{
if (p->right != NULL){
p = p->right;
} else {
p->right = (BTree *)malloc(sizeof(BTree));
p = p->right;
p->istail = 0;
p->left = p->right = NULL;
}
}
++i;
}
p->istail = 1;
} int isImmediately(BTree * root){
BTree * p = root;
while (p != NULL){
if (p->istail == 1 && (p->left != NULL || p->right != NULL))
return 0;
else
return isImmediately(p->left) && isImmediately(p->right);
}
return 1;
} void destoryBTree(pBTree * root){
if ((*root)->left)
destoryBTree(&(*root)->left);
if ((*root)->right)
destoryBTree(&(*root)->right);
free(*root);
*root = NULL;
} int main(void){
int count = 0;
while (gets(data)){
if (data[0] == '9'){
if (isImmediately(root))
printf("Set %d is immediately decodable\n", ++count);
else
printf("Set %d is not immediately decodable\n", ++count);
destoryBTree(&root);
} else{
insert(data);
}
} return 0;
}

POJ1056 IMMEDIATE DECODABILITY【数据结构】的更多相关文章

  1. POJ1056 IMMEDIATE DECODABILITY & POJ3630 Phone List

    题目来源:http://poj.org/problem?id=1056   http://poj.org/problem?id=3630 两题非常类似,所以在这里一并做了. 1056题目大意: 如果一 ...

  2. POJ--1056 IMMEDIATE DECODABILITY && POJ--3630 Phone List(字典树)

    题目链接 题目大意 看输入的每个字符串中是否有一个字符串是另一个字符串的前缀 #include<iostream> #include<cstring> #include< ...

  3. 多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)

    前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...

  4. 一起学 Java(三) 集合框架、数据结构、泛型

    一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...

  5. 深入浅出Redis-redis底层数据结构(上)

    1.概述 相信使用过Redis 的各位同学都很清楚,Redis 是一个基于键值对(key-value)的分布式存储系统,与Memcached类似,却优于Memcached的一个高性能的key-valu ...

  6. 算法与数据结构(十五) 归并排序(Swift 3.0版)

    上篇博客我们主要聊了堆排序的相关内容,本篇博客,我们就来聊一下归并排序的相关内容.归并排序主要用了分治法的思想,在归并排序中,将我们需要排序的数组进行拆分,将其拆分的足够小.当拆分的数组中只有一个元素 ...

  7. 算法与数据结构(十三) 冒泡排序、插入排序、希尔排序、选择排序(Swift3.0版)

    本篇博客中的代码实现依然采用Swift3.0来实现.在前几篇博客连续的介绍了关于查找的相关内容, 大约包括线性数据结构的顺序查找.折半查找.插值查找.Fibonacci查找,还包括数结构的二叉排序树以 ...

  8. 算法与数据结构(九) 查找表的顺序查找、折半查找、插值查找以及Fibonacci查找

    今天这篇博客就聊聊几种常见的查找算法,当然本篇博客只是涉及了部分查找算法,接下来的几篇博客中都将会介绍关于查找的相关内容.本篇博客主要介绍查找表的顺序查找.折半查找.插值查找以及Fibonacci查找 ...

  9. 算法与数据结构(八) AOV网的关键路径

    上篇博客我们介绍了AOV网的拓扑序列,请参考<数据结构(七) AOV网的拓扑排序(Swift面向对象版)>.拓扑序列中包括项目的每个结点,沿着拓扑序列将项目进行下去是肯定可以将项目完成的, ...

随机推荐

  1. 小凡的Linux主机与时间服务器同步记录

    小凡的Linux主机与时间服务器同步记录 导读 我们新安装的Linux主机,如果没有做与互联网服务器时间同步的处理的话,当我们使用date命令的时候,我们就看不到当前的时间,只能看到过去的时间.在我们 ...

  2. ImageSource使用心得(转)

    很多时候,我们会使用图片来装饰UI,比如作为控件背景等. 而这些图片可以分为两种形式,即存在于本地文件系统中的图片和存在于内存中的图片 对于这两种形式的图片,在WPF中,使用方法不同,下面主要说明针对 ...

  3. [JavaEE] Eclipse中web-inf和meta-inf文件夹的信息

    WEB-INF    /WEB-INF/web.xml        你的Web应用程序配置文件,这是一个XML文件,其中描述了 servlet 和其他的应用组件配置及命名规则:    /WEB- I ...

  4. 获取客户端的IP地址

    /// <summary> /// 获取客户端的IP地址 /// </summary> /// <returns></returns> public s ...

  5. ProcMon启用调试符

    1.设置 _NT_SYMBOL_PATH 如果在 _NT_SYMBOL_PATH 环境变量中提供了正确的?symsrv?语法,则常见的 Mircoroft 调试工具将使用 SymSrv 技术.这些工具 ...

  6. 【Android Studio使用教程 7】AndroidStudio问题汇总

    FindBugs:Compiler output path for module can not be null. check your module/project settings问题原因 And ...

  7. Plupload文件上传组件使用API

    Plupload有以下功能和特点: 1.拥有多种上传方式:HTML5.flash.silverlight以及传统的<input type=”file” />.Plupload会自动侦测当前 ...

  8. Flips测试类(page43)

    测试用例:所用java类: StdOut,StdIn , Counter, StdRandom, public class Flips { public static void main(String ...

  9. node.js学习的资源整理

    node中文社区 Node.js专业中文社区:https://cnodejs.org/ node文档 node.js 中文api :http://nodeapi.ucdok.com/ node.js入 ...

  10. CentOS中ps配合Kill进程的N种方法

    首先,用ps查看进程,方法如下: $ ps -ef  …… smx       1822     1  0 11:38 ?        00:00:49 gnome-terminal smx     ...