17.04.29 Bacon's Cipher

来源:互联网 发布:卡尔曼 知乎 编辑:程序博客网 时间:2024/06/05 14:51

Bacon's Cipher

 HDU - 4144 

Bacon's cipher or the Baconian cipher is a method of steganography (a method of hiding a secret message as opposed to a true cipher) devised by Francis Bacon. A message is concealed in the presentation of text, rather than its content. 
As we all know, each letter has its position in the alphabet, ‘A’ is 0, ‘B’ is 1, ‘C’ is 2…and so on. And each number can be represented in binary code, for example, 2 is ‘10’ in binary system. Then we expand the binary code to five digits by adding leading zeros, then 10 becomes 00010. Now we can use this number to encode. To simplify the question, we define the rules as below: 
0 corresponds to a random uppercase letter and 1 corresponds to a random number, so after encoding, 00010 ( ‘C’ ) is transformed to ABC1D or JUG9N. 
To decode, do the opposite way around. 

Input

The first line contains a positive number l, represents the length of the encoded string. L<=10000 and can be divided by 5. The second line is the encoded string.

Output

The original string.

Sample Input

35

ON1E2H5Q39AK2TGIC9ERT39B2P423L8B20D

Sample Output

FLEENOW


————————————

读一个二维字符串,每行五个字符,一行转换成一串二进制,再将二进制转成十进制,再输出字符。

一开始分割字符串的时候总是出错,是因为scanf("%s",ss)读了input第一行末尾的\n,醉了。


#include <stdio.h>#include <math.h>#include <algorithm>using namespace std;int to10(char ss[5]){    int res=0,i;    for(i=4;i>=0;i--)    {        res+=ss[i]*pow(2,(4-i));    }    return res;}int main(){    int n;    while(~scanf("%d\n",&n))    {    char a[2001][5];    for(int i=0;i<(n/5);i++)    {        int j;        for(j=0;j<5;j++)        {            scanf("%c",&a[i][j]);            if(a[i][j]>='A'&&a[i][j]<='Z')a[i][j]=0;            else a[i][j]=1;        }        printf("%c",to10(a[i])+'A');    }    printf("\n");     }    return 0;}



0 0
原创粉丝点击