IMMEDIATE DECODABILITY
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9630   Accepted: 4555

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
题目大意:给定一段编码,每段编码以“9”结束,判断是否有一个编码是另一个编码的前缀。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std; typedef struct node
{
int n;
node *next[];
node()
{
for (int i = ; i < ; i++)
{
next[i] = NULL;
}
n = ;
}
}TreeNode; void Insert(char str[], TreeNode *pHead)
{
TreeNode *p = pHead;
int nLen = strlen(str);
for (int i = ; i < nLen; i++)
{
if (p->next[str[i] - ''] == NULL)
{
p->next[str[i] - ''] = new TreeNode;
}
else
{
p->next[str[i] - '']->n++;
}
p = p->next[str[i] - ''];
}
} int Search(char str[], TreeNode *pHead)
{
int nLen = strlen(str);
TreeNode *p = pHead;
bool bfind = false;
for (int i = ; i < nLen; i++)
{
p = p->next[str[i] - ''];
}
return p->n;
} void Delete(TreeNode *pHead)
{
for (int i = ; i < ; i++)
{
if (pHead != NULL)
{
pHead = pHead->next[i];
Delete(pHead);
}
}
delete pHead;
} int main()
{
char str[][];
int nCase = ;
int n = -;
TreeNode *pHead = new TreeNode;
int flag = ;
while(scanf("%s", str[++n]) != EOF)
{
if (str[n][] == '')
{
++nCase;
for (int i = ; i < n ; i++)
{
if (Search(str[i], pHead) > )
{
printf("Set %d is not immediately decodable\n", nCase);
break;
}
if (i == n - )
{
printf("Set %d is immediately decodable\n", nCase);
}
}
Delete(pHead);
pHead = new TreeNode;
n = -;
}
else
{
Insert(str[n], pHead);
}
}
return ;
}

POJ 1056 IMMEDIATE DECODABILITY的更多相关文章

  1. poj 1056 IMMEDIATE DECODABILITY(KMP)

    题目链接:http://poj.org/problem?id=1056 思路分析:检测某字符串是否为另一字符串的前缀,数据很弱,可以使用暴力解法.这里为了练习KMP算法使用了KMP算法. 代码如下: ...

  2. poj 1056 IMMEDIATE DECODABILITY 字典树

    题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...

  3. POJ 1056 IMMEDIATE DECODABILITY 【Trie树】

    <题目链接> 题目大意:给你几段只包含0,1的序列,判断这几段序列中,是否存在至少一段序列是另一段序列的前缀. 解题分析: Trie树水题,只需要在每次插入字符串,并且在Trie树上创建节 ...

  4. POJ 1056 IMMEDIATE DECODABILITY Trie 字符串前缀查找

    POJ1056 给定若干个字符串的集合 判断每个集合中是否有某个字符串是其他某个字符串的前缀 (哈夫曼编码有这个要求) 简单的过一遍Trie就可以了 #include<iostream> ...

  5. 【POJ】1056 IMMEDIATE DECODABILITY

    字典树水题. #include <cstdio> #include <cstring> #include <cstdlib> typedef struct Trie ...

  6. 1056 IMMEDIATE DECODABILITY

    题目链接: http://poj.org/problem?id=1056 题意: 给定编码集, 判断它是否为可解码(没有任何一个编码是其他编码的前缀). 分析: 简单题目, 遍历一遍即可, 只需判断两 ...

  7. POJ 1056

    #include <iostream> #include <string> #define MAXN 50 using namespace std; struct node { ...

  8. POJ题目排序的Java程序

    POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...

  9. 蓝书2.3 Trie字典树

    T1 IMMEDIATE DECODABILITY poj 1056 题目大意: 一些数字串 求是否存在一个串是另一个串的前缀 思路: 对于所有串经过的点权+1 如果一个点的end被访问过或经过一个被 ...

随机推荐

  1. js实现接口的几种方式

    Javascript模仿接口可以有三种方式:1.注释法 2.检查属性法 3.鸭式辨形法 1.注释法:此方法属于程序文档范畴,对接口的继承实现完全依靠程序员自觉 /* interface People{ ...

  2. React项目搭建(脚手架)

    首先我们需要安装node环境:download nodejs:https://i.cnblogs.com/EditPosts.aspx?opt=1 找到你需要的版本和系统安装包下载并安装. 这时候你可 ...

  3. 3、从尾到头打印链表------------>剑指offer系列

    题目 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 分析 要了解链表的数据结构: val属性存储当前的值,next属性存储下一个节点的引用. 要遍历链表就是不断找到当前节点的nex ...

  4. 浅析 var that = this;

    在阅读别人的代码时,发现别人写的代码中有这么一句:var that = this;,这代表什么意思呢?经过一番查阅,才明白是这么回事. 在JavaScript中,this代表的是当前对象. var t ...

  5. Android Studio你必须学会的快捷键(Eclipse转AS必看)

    前言:从Eclipse转到Android Studio之后,一开始把keymap设置成Eclipse,却发现有些常用的快捷键都失效了,大概是冲突了.想了下,觉得与其重新设置快捷键,不如去适应AS的快捷 ...

  6. Android 权限的实现

    1.    权限 每个程序在安装时都有建立一个系统ID,如app_15,用以保护数据不被其它应用获取.Android根据不同的用户和组,分配不同权限,比如访问SD卡,访问网络等等.底层映射为Linux ...

  7. Python安装第三方库文件工具——pip

    Python安装第三方库文件一般使用pip. 1.pip的安装 (1)下载pip 进入https://pypi.python.org/pypi/pip#downloads

  8. ubuntu 14.04 构建openstack使用的ubunt 16 的桌面版的使用镜像

    1. 下载ubuntu 16.04桌面版的iso文件,我的个人网盘中有,可以下载 https://pan.baidu.com/s/14qT3lbbqLwDaejmz2VSkyw 2. 安装制作镜像文件 ...

  9. 万事先问『为什么』 what why how

    万事先问『为什么』! 遇到问题时,很多人的行为模式顺序是,先问『做什么』,『怎么做』,他们从来不问『为什么』,他们对根源性问题很模糊. 而聪明人则是先问『为什么』,再去构建『怎么做』,而『做什么』就是 ...

  10. 团队作业-Beta冲刺第三天

    这个作业属于哪个课程 <https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1> 这个作业要求在哪里 <https ...