题目地址: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. Qt组件中的双缓冲无闪烁绘图

      双缓冲绘图在Qt4中,所有的窗口部件默认都使用双缓冲进行绘图.使用双缓冲,可以减轻绘制的闪烁感.在有些情况下,用户要关闭双缓冲,自己管理绘图.下面的语句设置了窗口部件的Qt::WA_PaintOn ...

  2. PHP header 的几种用法

    跳转页面 header('Location:'.$url); //Location和":"之间无空格. 声明content-type header('content-type:te ...

  3. Design Mode 之 行为模式

    行为型模式,共十一种:策略模式.模板方法模式.观察者模式.迭代子模式.责任链模式.命令模式.备忘录模式.状态模式.访问者模式.中介者模式.解释器模式. 看看这11中模式的关系,大致可分为四类:(1) ...

  4. Wcf 之 配置文件解析

    在WCF Service Configuration Editor的使用中,我们通过配置工具自动生成了WCF服务端的config文件.现在我们来看下这个配置文件各个标签的意义(解释在下面xml文件中的 ...

  5. Blocks 推出矩阵公式。矩阵快速密

    Blocks 设涂到第I块时,颜色A,B都为偶数的数量为ai,一奇一偶的数量为bi,都为奇数为ci,  那么涂到第i+1快时有 a[i+1]=2*a[i]+b[i]+0*c[i]; b[i+1]=2* ...

  6. 【Shell脚本学习4】几种常见的Shell

    上面提到过,Shell是一种脚本语言,那么,就必须有解释器来执行这些脚本. Unix/Linux上常见的Shell脚本解释器有bash.sh.csh.ksh等,习惯上把它们称作一种Shell.我们常说 ...

  7. c#入门实例

    1.概述 C#是一个语言,.net是一个平台,上面支持用C#或者VB.Net写代码 2.注释 若注释量较少用  //   开头,大量用   /*    */    表示 输出结果 3.命名空间 所谓n ...

  8. Umbraco(7)-The Navigation Menu And A Parent Page with Infinite Children

    原文链接:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/umbraco7the-navigation-menu-and-a-par ...

  9. CF Soldier and Cards (模拟)

    Soldier and Cards time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  10. 【求无向图的桥,有重边】ZOJ - 2588 Burning Bridges

    模板题——求割点与桥 题意,要使一个无向图不连通,输出必定要删掉的边的数量及其编号.求桥的裸题,可拿来练手. 套模板的时候注意本题两节点之间可能有多条边,而模板是不判重边的,所以直接套模板的话,会将重 ...