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. 2840 WIKIOI——评测

    2840 WIKIOI——评测 时间限制: 1 s 空间限制: 2000 KB 题目等级 : 白银 Silver       题目描述 Description Wikioi上有一题有N个测试点,时限为 ...

  2. 10个优秀的移动Web应用开发框架

    在最近几年里,移动互联网高速发展.市场潜力巨大.继计算机.互联网之后,移动互联网正掀起第三次信息技术革命的浪潮,新技术.新应用不断涌现.今天这篇文章向大家推荐10大优秀的移动Web开发框架,帮助开发者 ...

  3. Python 元组、字典、集合操作总结

    元组 a=('a',) a=('a','b') 特点 有序 不可变,不可以修改元组的值,无法为元组增加或者删除元素 元组的创建 a=('a',) a=('a','b') tuple('abcd') 转 ...

  4. js 前端不调接口直接下载图片

    // 下载图片 downPhoto (path) { this.downloadFiles(path) }, // 下载 downloadFiles (content) { console.log(c ...

  5. iphone在jsp显示时间会NAN解决办法

    例:2018-12-28 15:00:00 1.   var  newDate = new Date("2018-12-28 15:00:00") 这种获取的时间在安卓手机上显示是 ...

  6. OpenCascade: 获取边的端点

    FirstV = TopExp::FirstVertex(aEdge1); LastV = TopExp::LastVertex(aEdge1);

  7. 【转】IntelliJ 创建main函数快捷

    http://blog.csdn.net/tiantiandjava/article/details/42269173 今天偶然发现了IntelliJ中 创建main函数的快捷键,依次还有for循环, ...

  8. java中regex参考

    在Sun的Java JDK 1.40版本中,Java自带了支持正则表达式的包,本文就抛砖引玉地介绍了如何使用java.util.regex包. 可粗略估计一下,除了偶尔用Linux的外,其他Linu ...

  9. HTML5拖放(drag和drog)作品

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  10. java在线聊天项目 实现基本聊天功能后补充的其他功能详细需求分析 及所需要掌握的Java知识基础 SWT的激活方法,swt开发包下载,及破解激活码

    补充聊天项目功能,做如下需求分析: 梳理项目开发所需的必要Java知识基础 GUI将使用更快速的swt实现 SWT(Standard Widget Toolkit) Standard Widget T ...