Problem 022 —— UVa 213 - Message Decoding

本文介绍了一种特殊的二进制消息编码方案,并提供了一个能够解析这种编码的程序实现。该方案通过将消息分为头部和模式两部分进行编码,其中头部包含消息字符,模式则由一系列0和1构成的键值对应到头部字符。文章详细解释了编码原理及如何通过程序进行解码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Message Decoding 

Some message encoding schemes require that an encoded message be sent in two parts. The first part, called the header, contains the characters of the message. The second part contains a pattern that represents the message. You must write a program that can decode messages under such a scheme.

The heart of the encoding scheme for your program is a sequence of ``key" strings of 0's and 1's as follows:

displaymath26

The first key in the sequence is of length 1, the next 3 are of length 2, the next 7 of length 3, the next 15 of length 4, etc. If two adjacent keys have the same length, the second can be obtained from the first by adding 1 (base 2). Notice that there are no keys in the sequence that consist only of 1's.

The keys are mapped to the characters in the header in order. That is, the first key (0) is mapped to the first character in the header, the second key (00) to the second character in the header, the kth key is mapped to the kth character in the header. For example, suppose the header is:

AB#TANCnrtXc

Then 0 is mapped to A, 00 to B, 01 to #, 10 to T, 000 to A, ..., 110 to X, and 0000 to c.

The encoded message contains only 0's and 1's and possibly carriage returns, which are to be ignored. The message is divided into segments. The first 3 digits of a segment give the binary representation of the length of the keys in the segment. For example, if the first 3 digits are 010, then the remainder of the segment consists of keys of length 2 (00, 01, or 10). The end of the segment is a string of 1's which is the same length as the length of the keys in the segment. So a segment of keys of length 2 is terminated by 11. The entire encoded message is terminated by 000 (which would signify a segment in which the keys have length 0). The message is decoded by translating the keys in the segments one-at-a-time into the header characters to which they have been mapped.

Input

The input file contains several data sets. Each data set consists of a header, which is on a single line by itself, and a message, which may extend over several lines. The length of the header is limited only by the fact that key strings have a maximum length of 7 (111 in binary). If there are multiple copies of a character in a header, then several keys will map to that character. The encoded message contains only 0's and 1's, and it is a legitimate encoding according to the described scheme. That is, the message segments begin with the 3-digit length sequence and end with the appropriate sequence of 1's. The keys in any given segment are all of the same length, and they all correspond to characters in the header. The message is terminated by 000.

Carriage returns may appear anywhere within the message part. They are not to be considered as part of the message.

Output

For each data set, your program must write its decoded message on a separate line. There should not be blank lines between messages.

Sample input

TNM AEIOU
0010101100011
1010001001110110011
11000
$#**\
0100000101101100011100101000

Sample output

TAN ME
##*\$

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>

char s[100008];

int lenth(int i)
{
    int a=(s[i]-'0')*4+(s[i+1]-'0')*2+(s[i+2]-'0');
    if(s[i]=='\0') return 0;
    return a;
}

int num(int j,int l)
{
    int a=0,pd=1;
    int p=1;
    int L=l;
    for(l--;l>=0;l--)
    {
        a+=(s[j+l]-'0')*p;
        p*=2;
        if(s[j+l]=='0') pd=0;
    }

    for(int i=1;i<L;i++)
        a+=(pow(2,i)-1);
    if(pd==1) return -1;
    return a;
}

int main()
{
    char str[108];
    while(gets(str)!=NULL)
    {
        char a[108];
        int len=0;
        memset(s,0,sizeof(s));
        while(gets(a))
        {
            strcat(s,a);
            len=strlen(s);
            if(s[len-1]==s[len-2]&&s[len-2]==s[len-3]&&s[len-1]=='0')
                break;
        }
        for(int i=0;i<len;)
        {
            int l=lenth(i);
            if(l==0) break;
            i+=3;
            for(int j=i;j<len;i+=l,j+=l)
            {
                int L=num(j,l);
                if(L==-1) break;
                printf("%c",str[L]);
            }
            i+=l;
        }
        printf("\n");
    }
    return 0;
}





### 位树解码(Bit-tree Decoding)的工作原理 位树解码是一种高效的解码方法,主要用于处理具有特定范围的无符号整数值。其核心思想是利用二叉树结构来表示可能的数值分布,并通过逐比特解析输入流中的数据完成解码过程。 #### 基本概念 在位树解码过程中,假设目标是从输入比特流中提取一个小于某个限制值 \( L \) 的无符号整数。该过程可以通过构建一棵虚拟的二叉树来实现,其中每条路径代表一个具体的数值[^1]。具体而言: - **根节点**:表示初始状态。 - **分支方向**:左子节点通常对应较低的数值区间,而右子节点则扩展到更高的数值区域。 - **终止条件**:当遍历到达某一层时,若当前累积值已达到或超过预设的最大限值,则停止继续深入并返回最终结果。 这种机制能够显著减少对于较大数值编码所需的平均比特数量,从而提升整体传输效率。 ```python def bit_tree_decode(bit_stream, limit): value = 0 while True: next_bit = read_next_bit_from(bit_stream) if not next_bit or value >= limit - 1: break value = (value << 1) | next_bit return min(value, limit - 1) ``` 上述伪代码展示了基于位树策略的一个简单实现方式。它持续从`bit_stream`读取单个比特直到满足退出准则为止;值得注意的是,在实际应用场景下还需要考虑边界情况以及错误检测等问题[^2]。 #### 应用场景与优势对比其他技术方案 相比于传统的固定长度或者可变长度编码手段来说,采用位树方式进行解码具备如下几个方面的优点: - 更加灵活适应不同统计特性的源信号; - 能够有效降低高频部分所需存储空间占用比例; - 特别适合那些经过DCT变换后再经历Z字形排列后的图像压缩领域内使用[^3]。 然而需要注意的是,尽管这种方法理论上看起来非常理想化,但在某些极端情况下可能会导致性能下降甚至失败的结果发生。因此,在设计具体算法之前应当充分评估待处理数据集的特点及其潜在局限性所在。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值