Problem Description
In cryptography, the goal is to encrypt a message so that, even if the the message is intercepted, only the intended recipient can decrypt it. In steganography, which literally means “hidden writing”, the goal is to hide
the fact that a message has even been sent. It has been in use since 440 BC. Historical methods of steganography include invisible inks and tatooing messages on messengers where they can’t easily be seen. A modern method is to encode a message using the least-significant bits of the RGB color values of pixels in a digital image.
For this problem you will uncover messages hidden in plain text. The spaces within the text encode bits; an odd number of consecutive spaces encodes a 0 and an even number of consecutive spaces encodes a 1. The
four texts in the example input below (terminated by asterisks) encode the following bit strings: 11111, 000010001101101, 01, and 000100010100010011111. Each group of five consecutive bits represents a
binary number in the range 0–31, which is converted to a character according to the table below. If the last group contains fewer than five bits, it is padded on the right with 0’s.
" " (space) 0
“A” – “Z” 1–26
“’” (apostrophe) 27
“,” (comma) 28
“-” (hyphen) 29
“.” (period) 30
“?” (question mark) 31
The first message is 111112 = 3110 = “?”. The second message is (00001, 00011, 01101)2 = (1, 3, 13)10 =
“ACM”. The third message is 010002 = 810 = “H”, where the underlined 0’s are padding bits. The fourth message is (00010, 00101, 00010, 01111, 10000)2 = (2, 5, 2, 15, 16)10 = “BEBOP”.
Input
The input consists of one or more texts. Each text contains one or more lines, each of length at most 80 characters, followed by a line containing only “" (an asterisk) that signals the end of the text. A line containing only “#” signals the end of the input. In addition to spaces, text lines may contain any ASCII letters, digits, or punctuation, except for "” and “#”, which are used only as sentinels.
Output
For each input text, output the hidden message on a line by itself. Hidden messages will be 1–64 characters long.
Note: Input text lines and output message lines conform to all of the whitespace rules listed in item 7 of Notes to Teams except that there may be consecutive spaces within a line. There will be no spaces at the beginning or end of a line.
Sample Input
Programmer,
I would like to see
a question
mark.
*
Behold, there is more to me than you might
think when you read me the first time.
*
Symbol for hydrogen?
*
A B C D E F G H I J K L M N O P Q R S T U V
*
Sample Output
?
ACM
H
BEBOP
Source
2008 Mid-Central USA
题意:
在一篇文章中,每一段连续的空格代表一个0或一个1。偶数个代表1,奇数个则为0。把所有空格连起来得到一串0-1组成的二进制,再进行解密。每5个0-1二进制字符对应1个字母,末尾不足5个补零。二进制对应的十进制中,0代表空格,1代表A,2代表B……26代表Z,之后27到31分别代表’,-.?。按要求解密文章。
#include<iostream>
#include<math.h>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
char x[32]={' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',' ',',','-','.','?'};
char s[8000];
int texts[65],text[500],op,yp;
void addchar(char k[]){
strcpy(s+strlen(s),k);
}//连接成一段文字
void puttext(){
int i,j,tp=0;
op=0;
for(i=0;i<strlen(s);){
tp=1;
if(s[i]==' '){
for(j=i+1;j<strlen(s);j++){
if(s[j]==' ')
tp++;
else
break;
}
if(tp%2==0)
text[op++]=1;
else
text[op++]=0;
}
i+=tp;
}
}//解密成二进制
void make(){
int i,y=0,f=4;
yp=0;
for(i=0;i<op;i++){
y=y+text[i]*pow(2,f);
if(f==0||(i==op-1)){
texts[yp++]=y;
f=4;
y=0;
}
else
f--;
}
}//将解密后的二进制化为十进制
void print(){
int i;
for(i=0;i<yp;i++){
if(i>64)
break;
if(texts[i]==0)
cout<<"";
else if(texts[i]==27)
cout<<"'";
else
cout<<x[texts[i]];
}
cout<<endl;
}//输出结果
int main(){
char k[80];
while(gets(k)){
if(k[0]=='#')
break;
else{
if(k[0]!='*')
addchar(k);
else{
puttext();
make();
print();
memset(s,'\0',sizeof(s));
memset(text,0,sizeof(text));
memset(texts,0,sizeof(texts));//记得清除结果
}
}
}
return 0;
}