题目地址: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. GIT分支管理是一门艺术

    英文原文:http://www.nvie.com/posts/a-successful-git-branching-model/ 原文作者:Vincent Driessen 本文经Linux大棚博主总 ...

  2. QtInternal 之 高效使用QString

    注意:本文翻译自  http://developer.qt.nokia.com   中的  UsingQStringEffectively   ,中文译文见  简体中文版 ,如果你对翻译wiki感兴趣 ...

  3. 经典排序算法总结与实现 ---python

    原文:http://wuchong.me/blog/2014/02/09/algorithm-sort-summary/ 经典排序算法在面试中占有很大的比重,也是基础,为了未雨绸缪,在寒假里整理并用P ...

  4. LeetCode3 Longest Substring Without Repeating Characters

    题意: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  5. Quoit Design

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission ...

  6. [WinForm] TableLayoutPanel和FlowLayoutPanel的使用

    这篇文章主要跟大家分享下,在配餐系统的开发中,对tableLayoutPanel 和 flowLayoutPanel 控件的使用方法和技巧 ——后附上 测试demo, 相信需要的朋友下载看后能很快的知 ...

  7. 安装TortoiseGit出现提示“您必须安装带有更新版本Windows Installer服务的Windows Service Pack”-解决方法

    我的系统是xp sp3安装TortoiseGit时出现了错误提示“您必须安装带有更新版本Windows Installer服务的Windows Service Pack”. 解决方法,到微软官方下载相 ...

  8. 浅谈C#中ref与out的区别

    在C#这门高级语言中,你是否注意过ref与out的用法?你是否为在调用方法时需要多个返回值呢?不用急,接下来,我们去一起去研究一下这个问题... 其实呢,C#语言中,参数的传递一共有两种方法,值传递和 ...

  9. 【Dijkstra】

    [摘自]:华山大师兄,推荐他的过程动画~   myth_HG 定义 Dijkstra算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩 ...

  10. Linux勉強

        block 與 inode 的總量: 未使用與已使用的 inode / block 數量: block 與 inode 的大小 (block 為 1, 2, 4K,inode 為 128 by ...