题目地址: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. JAVA+FlexPaper+OpenOffice+SWFTools文档预览

    http://blog.csdn.net/core_star/article/details/10148047 1.软件环境: openoffice:启动openoffice服务:soffice.ex ...

  2. eclipse[downloads]

    下载J2EE:http://www.eclipse.org/downloads/ 下载WPT插件:http://download.eclipse.org/webtools/updates 下载TOMC ...

  3. ImageView的Scaletype

    ImageView的Scaletype决定了图片在View上显示时的样子,如进行何种比例的缩放,及显示图片的整体还是部分,等等. 设置的方式包括: 1. 在layout xml中定义android:s ...

  4. Android(java)学习笔记66:实现Runnable接口创建线程 和 使用Callable和Future创建线程

    1. 前面说的线程的实现是新写一个子类继承Thread: 是将类声明为 Thread 的子类.该子类应重写 Thread 类的 run 方法.接下来可以分配并启动该子类的实例 2. 这里说的方案2是指 ...

  5. Windows8不联网直接安装.Net 3.5 Framework的方法

    把你的系统ISO加载到虚拟光驱或插入系统安装盘,找到X:\sources\sxs路径(X是你的光驱盘符).输入下面命令,盘符以D盘为例DISM /Online /Enable-Feature /Fea ...

  6. Oracle基础<4>--程序包

    一:程序包定义(包括1.程序包规范 2.程序包主体) 程序包是一种数据库对象,它是对相关pl/sql 类型.子程序.游标.异常.变量和常量的封装. 1.程序包规范:可以声明类型.变量.常量.异常.游标 ...

  7. ASP.NET入门(1) - 建立和开发ASP.NET 5 项目

    原文转载自:http://www.cnblogs.com/zergcom/p/4493358.html 建立项目 首先,目前只有VS 2015支持开发最新的ASP.NET 5 程序,所以我们首先需要下 ...

  8. CMP指令(转)

    刚刚看到了cmp指令,一开始有点晕.后来上网找了些资料,终于看明白了,为了方便初学者,我就简单写下我的思路吧.高手绕过,谢谢! cmp(compare)指令进行比较两个操作数的大小 例:cmp opr ...

  9. state/ui-router

    state/ui-router 一个状态对应于一个页面位置 通过定义controller.template和view等属性,来定义指定位置的用户界面和界面行为 通过嵌套的方式来解决页面中的一些重复出现 ...

  10. js中关于数据类型的转换

    * 一.转化为数字*/console.log(‘123123’*1.0); /* 二.从一个值中提取另一中类型的值,并完成转化工作 */console.log(parseInt(‘1233zxhag’ ...