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. Vuex.js状态管理共享数据 - day8

    VScode文件目录: amount.vue代码如下: <template> <div> <!-- <h3>{{ $store.state.count }}& ...

  2. PeopleSoft FSCM Production Support 案例分析

    PeopleSoft FSCM Production Support 案例分析 2010年的时候曾建言博客园开辟Oracle ERP模块供大家交流,博客园如约开辟Oracle ERP 模块,而我后来却 ...

  3. ios自定义日期、时间、城市选择器

    选择器,我想大家都不陌生,当需要用户去选择某些范围值内的一个固定值时,我们会采用选择器的方式.选择器可以直观的提示用户选择的值范围.统一信息的填写格式,同时也方便用户快速的进行选择,比如对于性别,正常 ...

  4. shell中使用ssh

    ssh服务是不能用非交互的方式传递密码,想不输入密码,直接ssh连接到服务器有两种方法,sshpass和expect sshpass # wget http://downloads.sourcefor ...

  5. shrio 权限管理filterChainDefinitions过滤器配置

    /** * Shiro-1.2.2内置的FilterChain * @see ============================================================= ...

  6. 【Qt】2.2 继续了解信号和槽

    槽和普通成员函数一样,可以是虚函数.被重载,可以是公有.私有.保护的.它可以被其它C++成员函数调用. 槽连接了信号,当发射这个信号时,槽会被自动调用. 连接函数: bool QObject::con ...

  7. shell脚本,按空格开始60秒的倒计时。

    [root@localhost wyb]# cat space.sh #!/bin/bash #按空格开始60秒的倒计时#-n表示接受字符的数量,1表示只接受一个字符  a() { - ` do ec ...

  8. javascript(九)事件冒泡 onmouseenter onmouseenter 默认事件 和 键盘事件

    1 事件冒泡 子元素触发的事件,会往上(父元素)传递: 例子: <div id="box"> <p></p> </div> < ...

  9. abaqus二次开发概述

    说明 abaqus二次开发概述 导语 用户子程序特点 abaqus用户程序接口与调用方式 abaqus用户子程序分类 常用用户子程序介绍 Refence 说明 本系列文章本人基本没有原创贡献,都是在学 ...

  10. 【动态规划】bzoj2298: [HAOI2011]problem a

    建模超级妙…… Description 一次考试共有n个人参加,第i个人说:“有ai个人分数比我高,bi个人分数比我低.”问最少有几个人没有说真话(可能有相同的分数) Input 第一行一个整数n,接 ...