紫书第一章训练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][第一章 计算机网络和因特网] 标签(空格分隔): 计算机网络 介绍基本术语和概念 查看构成网络的基本硬件和软件组件. 从网络的边缘开始,考察在网络中运行的端 ...
随机推荐
- cocos2d-android-1学习之旅01
学习cocos2d-android-1也大概有半个月了,来整理一下自己的学习心得和提出自己的疑问.之所以不学习非常火的cocos2d-x,转而来学习这个网上学习资料少得可怜的cocos2d-andro ...
- Selenium3+webdriver学习笔记3(xpath方式元素定位)
#!/usr/bin/env python# -*- coding:utf-8 -*- from selenium import webdriver import time,os # about:ad ...
- Python + selenium之组织unittest单元测试用例
当增加被测功能和相应的测试用例之后unittest单元测试框架如何扩展和组织新增的测试用例的. # coding =utf-8 # calculator class Count (): def __i ...
- SingletonLoginUser
package cn.com.jgt.view{ import flash.errors.IllegalOperationError; /** * actionscript类的构造方法不能是priva ...
- 洛谷 P2014 选课
题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学分,每门课有一 ...
- iOS消息体系架构详解-融云即时通讯云
iOS SDK 体系架构 本文档将详细介绍融云的 SDK 产品架构和消息体系,以便于您更深入的了解融云并更快速的开发自己的产品. 融云 SDK 系统架构 IMKit IMKit 的功能主要是封装各种界 ...
- 【转】HTTP Live Streaming直播(iOS直播)技术分析与实现
HTTP Live Streaming直播(iOS直播)技术分析与实现 不经意间发现,大半年没写博客了,自觉汗颜.实则2012后半年,家中的事一样接着一样发生,实在是没有时间.快过年了,总算忙里偷闲, ...
- 导入文件 服务器报错,有可能是 开发时候是window 服务器是linux,两个系统的文件系统的/和\是相反的,要注意这块
导入文件 服务器报错,有可能是 开发时候是window 服务器是linux,两个系统的文件系统的/和\是相反的,要注意这块
- BCB:UTF8Encode、AnsiToUtf8
UTF8Encode: Call Utf8Encode to convert a Unicode string to UTF-8. WS is the Unicode string to conver ...
- JTT808、JTT809、JTT796、JTT794、JTT1077、JTT1078区别与交通部道路运输车辆卫星定位系统部标标准大全下载地址
部标JT/T808协议.JT/T809协议.JT/T796标准.JT/T794标准的区别,他们是基于不同的通信场景,不同的通信对象,不同的设计目的和目标而制定出来的.首先要知道这些标准的全称是什么意思 ...