poj 1056 IMMEDIATE DECODABILITY

ACM比赛整理

共 3461字,需浏览 7分钟

 · 2021-12-17

IMMEDIATE DECODABILITY


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



立即解码


描述

如果没有一个符号的代码是另一个符号的代码的前缀,则称一组符号的编码是可立即解码的。对于这个问题,我们将假设所有代码都是二进制的,一组代码中没有两个代码是相同的,每个代码至少有一位且不超过十位,并且每组代码至少有两个代码并且不超过八个。

示例:假设字母表具有符号 {A, B, C, D}

以下代码可立即解码:
A:01 B:10 C:0010 D:0000

但这个不是:
A:01 B:10 C:010 D:0000(注意A是C的前缀)

输入

编写一个程序,接受来自标准输入的一系列记录组作为输入。组中的每条记录都包含一组零和一,代表不同符号的二进制代码。每个组后跟一个包含单个 9 的分隔符记录;分隔符记录不是组的一部分。每个组独立于其他组;一组中的代码与任何其他组中的代码无关(即,每个组都将独立处理)。

输出

对于每个组,您的程序应确定该组中的代码是否可立即解码,并应打印一个输出行,给出组号并说明该组是否可立即解码。

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



代码:

#include 
#include
char tree[10000];
int cases;

void next()
{
printf("Set %d is not immediately decodable\n", ++cases);
while (getchar() != '9')
;
getchar();
memset(tree, -1, sizeof(tree));
}

int main()
{
int i = 2;
char c;

memset(tree, -1, sizeof(tree));
while ((c = getchar()) != EOF) {
if (c == '\n') {
if (tree[i] == '0' || tree[i+1] == '1') {
next();
}
tree[i] = tree[i+1] = '2';
i = 2;
}
else if (c == '9') {
getchar();
printf("Set %d is immediately decodable\n", ++cases);
memset(tree, -1, sizeof(tree));
i = 2;
}
else {
if (c == '0') {
if (tree[i] == '2') {
next();
i = 2;
}
else {
tree[i] = c;
i *= 2;
}
}
else if (c == '1') {
if (tree[++i] == '2') {
next();
i = 2;
}
else {
tree[i] = c;
i *= 2;
}
}
}
}
}


浏览 20
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报