紫书第一章训练1 D -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:
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的更多相关文章
- [OpenGL红宝书]第一章 OpenGL概述
第一章 OpenGL概述 标签(空格分隔): OpenGL 第一章 OpenGL概述 1 什么是OpenGL 2 初识OpenGL程序 3 OpenGL语法 4 OpenGL渲染管线 41 准备向Op ...
- [转] VS2015中跑OpenGL红宝书第八版的第一章示例代码,运行
Ori Article Link OpenGL的东西快忘光了,把角落的第八版红宝书拿出来复习一下 从书中的地址下了个示例代码结果新系统(Win10+VS2015)各种跑不起来,懊恼之后在网上疯狂搜索资 ...
- ROS机器人程序设计(原书第2版)补充资料 (壹) 第一章 ROS系统入门
ROS机器人程序设计(原书第2版)补充资料 (壹) 第一章 ROS系统入门 书中,大部分出现hydro的地方,直接替换为indigo或jade或kinetic,即可在对应版本中使用. 第一章主要包括R ...
- <<网络是怎样连接的>>笔记第一章browser生成message
网络:由负责搬运数字信息的机制 + 浏览器和服务器这些网络应用程序. web->协议栈,网卡->交换机,路由器->接入网,网络运营商->(防火墙,缓存服务器)->web服 ...
- 《第一本docker书》- 第一章笔记
环境: Ubuntu 14.04.2 LTS (GNU/Linux 3.16.0-30-generic i686) 第一章: 1 Docker客户端和服务器 2 Docker镜像 添加一个文件,执行一 ...
- Learning From Data 第一章总结
之前上了台大的机器学习基石课程,里面用的教材是<Learning from data>,最近看了看觉得不错,打算深入看下去,内容上和台大的课程差不太多,但是有些点讲的更深入,想了解课程里面 ...
- PRML读书会第一章 Introduction(机器学习基本概念、学习理论、模型选择、维灾等)
主讲人 常象宇 大家好,我是likrain,本来我和网神说的是我可以作为机动,大家不想讲哪里我可以试试,结果大家不想讲第一章.估计都是大神觉得第一章比较简单,所以就由我来吧.我的背景是统计与数学,稍懂 ...
- 翻译学python---《Learn Python the hard Way》---第一章 绪论
打算学习python,但是又不想单纯地看书或是写个小项目,干脆引入很流行的翻译学习法来学习吧- 在论坛上看到了国外的一本<Learn Python the hard Way> ...
- [A Top-Down Approach][第一章 计算机网络和因特网]
[A Top-Down Approach][第一章 计算机网络和因特网] 标签(空格分隔): 计算机网络 介绍基本术语和概念 查看构成网络的基本硬件和软件组件. 从网络的边缘开始,考察在网络中运行的端 ...
随机推荐
- ftp和sftp
一.ftp ftp是文件传输协议,ftp协议包括两部分,一个是ftp客户端,另一个是ftp服务器. 原理:一般情况下,当使用FTP服务的时候,我们都知道默认是21号端口,其实还有一个20号端口.FTP ...
- HTML视频简介
此文章复制原来文章,原网址是https://www.html5rocks.com/zh/tutorials/video/basics/#toc-encode 简介 视频标记是 HTML5 功能中备受关 ...
- oracle 11g r2安装
安装环境:windows 7 安装版本:Oracle_win32_11gR2 目的:用于模拟服务器环境,学习Oracle操作 1. 下载oracle 11g r2,下载地址:www.oracle.co ...
- 发现知乎的一个Bug,并且我绕过了此Bug,沾沾自喜中...
发现问题 在知乎点击修改头像,上传图片时发现一片空白.凭着程序员的直觉,第一反应时看下控制台是否有报错.果然发现如下: Refused to load the image 'data:image/jp ...
- 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest H Milestones1 (暴力)
预处理+暴力,每个颜色都是独立的,求个前缀和,减一减判断一个在区间内颜色是否存在. 算了算复杂度好像有点勉强,但是还是过了,学了主席树以后用主席树在做一下 #include<bits/stdc+ ...
- 常用的CSS居中方式
1.水平居中margin 0 auto;(浮动元素除外) 这个属性在网页制作的过程中是经常被用到的,一般情况下页面的版心你就可以看到它. <style> .father { width: ...
- SSL免费证书申请以及nginx配置https流程记录
设置https需要ssl 证书,可以通过FreeSSL[https://freessl.org/]申请. 流程记录: 输入域名,如 http://www.youdias.xin 选择品牌,如Let's ...
- LeetCode之Weekly Contest 92
第一题:转置矩阵 问题: 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9] ...
- STM32CUBEMX入门学习笔记1:软件的简单介绍
STM32CUBEMX是ST公司设计的一款免费软件,软件可以通过其官网下载.现在已经下载到.通过STM32CUBEMX可以完成从单片机选型,程序初始化,中断配置……工作.并生成对应的"HAL ...
- IOC容器和Bean的配置实例
实验1: <!--实验1:通过IOC容器创建对象,并为属性赋值 --> <!-- 需要由IOC容器创建对象的全类名 --> <!-- 为了便于从IOC容器中获取book对 ...