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:

0,00,01,10,000,001,010,011,100,101,110,0000,0001,...,1011,1110,00000,...

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\n##*$

这个题是真的有毒,怪不得大家没人做,其实就是字符串的处理。给你你要发的消息的文字,然后再按照每行2的n次方减一个进行转换。然后就是解决01串是干嘛的。"That is, the message segments begin with the 3-digit length sequence and end with the appropriate sequence of 1’s. "这句就是讲你接下来要扫描几位进行匹配,认识3就够了,三个字符的值代表行数,这几位的值代表列数,全是1就退出,段结束。扫三个字符确定新的行,因为不存在0行,000就退出这个字符串的处理,下一个字符串。

这个题01串会出现换行符,getchar会扫换行符,要处理下,最后这个要结束了肯定是一行的结束,继续getchar,要不读取不了下一位,调了好久好久才发现是这个错误。

我的代码仅供参考

<<是左移的意思,算关于2的很方便的,另外有时卡二分的时候,你用移位要比除法的速度快,移位还是强的,同理&1也是妙用的,判断其二进制位最后一位是不是1

#include<bits/stdc++.h>
char s[10][1025];
char t[520];
using namespace std;
void la(){
int n=1,m=0;
for(int i=0;t[i];i++){
s[n][m++]=t[i];
if(m+1==(1<<n)){
n++;
m=0;}
}
}
int main()
{while(gets(t)!=NULL){
la();
int sum=0;
for(int i=0;;i++){
char c;
c=getchar();
if(c=='\n')
c=getchar();
sum=sum*2+c-'0';
if(i%3==2&&sum==0)break;
if(i%3==2){
for(;;){
int s1=0;
for(int j=0;j<sum;j++){
char c;
c=getchar();
if(c=='\n')
c=getchar();
s1=2*s1+c-'0';}
if((s1+1)==1<<sum)
break;
else
printf("%c",s[sum][s1]);}
sum=0;}
}
printf("\n");
getchar();}
return 0;
}

紫书第一章训练1 D -Message Decoding的更多相关文章

  1. [OpenGL红宝书]第一章 OpenGL概述

    第一章 OpenGL概述 标签(空格分隔): OpenGL 第一章 OpenGL概述 1 什么是OpenGL 2 初识OpenGL程序 3 OpenGL语法 4 OpenGL渲染管线 41 准备向Op ...

  2. [转] VS2015中跑OpenGL红宝书第八版的第一章示例代码,运行

    Ori Article Link OpenGL的东西快忘光了,把角落的第八版红宝书拿出来复习一下 从书中的地址下了个示例代码结果新系统(Win10+VS2015)各种跑不起来,懊恼之后在网上疯狂搜索资 ...

  3. ROS机器人程序设计(原书第2版)补充资料 (壹) 第一章 ROS系统入门

    ROS机器人程序设计(原书第2版)补充资料 (壹) 第一章 ROS系统入门 书中,大部分出现hydro的地方,直接替换为indigo或jade或kinetic,即可在对应版本中使用. 第一章主要包括R ...

  4. <<网络是怎样连接的>>笔记第一章browser生成message

    网络:由负责搬运数字信息的机制 + 浏览器和服务器这些网络应用程序. web->协议栈,网卡->交换机,路由器->接入网,网络运营商->(防火墙,缓存服务器)->web服 ...

  5. 《第一本docker书》- 第一章笔记

    环境: Ubuntu 14.04.2 LTS (GNU/Linux 3.16.0-30-generic i686) 第一章: 1 Docker客户端和服务器 2 Docker镜像 添加一个文件,执行一 ...

  6. Learning From Data 第一章总结

    之前上了台大的机器学习基石课程,里面用的教材是<Learning from data>,最近看了看觉得不错,打算深入看下去,内容上和台大的课程差不太多,但是有些点讲的更深入,想了解课程里面 ...

  7. PRML读书会第一章 Introduction(机器学习基本概念、学习理论、模型选择、维灾等)

    主讲人 常象宇 大家好,我是likrain,本来我和网神说的是我可以作为机动,大家不想讲哪里我可以试试,结果大家不想讲第一章.估计都是大神觉得第一章比较简单,所以就由我来吧.我的背景是统计与数学,稍懂 ...

  8. 翻译学python---《Learn Python the hard Way》---第一章 绪论

    打算学习python,但是又不想单纯地看书或是写个小项目,干脆引入很流行的翻译学习法来学习吧-         在论坛上看到了国外的一本<Learn Python the hard Way> ...

  9. [A Top-Down Approach][第一章 计算机网络和因特网]

    [A Top-Down Approach][第一章 计算机网络和因特网] 标签(空格分隔): 计算机网络 介绍基本术语和概念 查看构成网络的基本硬件和软件组件. 从网络的边缘开始,考察在网络中运行的端 ...

随机推荐

  1. Android安卓电话拦截及短信过滤

    package com.focus.manager; import java.lang.reflect.Method; import Android .app.Activity; import And ...

  2. SharePoint Server和Office 365之间的混合模式集成概述

    正如您可能已经知道的那样,云中的Microsoft Office 365和SharePoint Server 2013/2016内部部署可以通过多种方式协同工作.这些通常被称为混合模式,因为它们将功能 ...

  3. 【TensorFlow入门完全指南】神经网络篇·MLP多层感知机

    前面的不做过多解释了. 这里定义了两个占位符,各位也知道,在训练时,feed_dict会填充它们. 定义相关网络. 这里是权值矩阵和偏差. 这里是实例化了网络,定义了优化器和损失,和上一篇一样. 最后 ...

  4. 1003: Redraiment的遭遇

    1003: Redraiment的遭遇 Time Limit: 1000 Sec  Memory Limit: 128 MBSubmit: 456  Solved: 158[Submit][Statu ...

  5. Collatz Conjecture

    4981: Collatz Conjecture 时间限制: 6 Sec  内存限制: 128 MB提交: 213  解决: 23[提交][状态][讨论版][命题人:admin] 题目描述 In 19 ...

  6. 2018.3.11 Java DEBUG 调试的方法

    F5:Step Into:跳进当前执行的方法中 F6:Step Over:跳过该行继续执行 F7:Step Return:从方法中跳出继续执行 F8:Resume:全部运行通过,从调试状态恢复 Ctr ...

  7. mongodb 导入导出

    F:\Mongodb\bin>mongoexport.exe -h localhost:27017 -d proxy_db -c proxy_tb -o f:/p1.json 导出 -h 服务器 ...

  8. scanf()的使用

    scanf函数称为格式输入函数,即按用户指定的格式从键盘上把数据输入到指定的变量之中. 如下面代码: #include<stdio.h> int main() { int a,b; sca ...

  9. The expected type was 'System.Int64' but the actual value was null.”

    System.InvalidOperationException:“An exception occurred while reading a database value for property ...

  10. JS与 JSON(一个菜鸟的不正经日常)

    今天学习了json的一些知识, 1 . 什么是json 1.1  JSON 英文全称 JavaScript Object Notation. 1.2  JSON 是一种轻量级的数据交换格式,用于存储和 ...