hdu1986 Encoding-字符串(电文加密)

Encoding

Problem Description
Chip and Dale have devised an encryption method to hide their (written) text messages. They first agree secretly on two numbers that will be used as the number of rows (R) and columns (C) in a matrix. The sender encodes an intermediate format using the following rules:
1. The text is formed with uppercase letters [A-Z] and <space>.
2. Each text character will be represented by decimal values as follows:
<space> = 0, A = 1, B = 2, C = 3, ..., Y = 25, Z = 26
The sender enters the 5 digit binary representation of the characters’ values in a spiral pattern along the matrix as shown below. The matrix is padded out with zeroes (0) to fill the matrix completely. For example, if the text to encode is: "ACM" and R=4 and C=4, the matrix would be filled in as follows:



The bits in the matrix are then concatenated together in row major order and sent to the receiver. The example above would be encoded as: 0000110100101100


 

Input
The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.
Each dataset consists of a single line of input containing R (1<=R<=20), a space, C (1<=C<=20), a space, and a text string consisting of uppercase letters [A-Z] and <space>. The length of the text string is guaranteed to be <= (R*C)/5.


 

Output
For each dataset, you should generate one line of output with the following values: The dataset number as a decimal integer (start counting at one), a space, and a string of binary digits (R*C) long describing the encoded text. The binary string represents the values used to fill in the matrix in rowmajor
order. You may have to fill out the matrix with zeroes (0) to complete the matrix.
 

Sample Input
  
  
4 4 4 ACM 5 2 HI 2 6 HI 5 5 HI HO
 

Sample Output
  
  
1 0000110100101100 2 0110000010 3 010000001001 4 0100001000011010110000010
 
题目大意
电文加密,加密方式是 用2进制 表示A~Z,然后用蛇形输入,然后正常输出

思路
把A~Z和空格,所有密码写出来,然后模拟蛇形操作就行。


代码

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const char a[27][6]={{"00000"},{"00001"},{"00010"},{"00011"},{"00100"},{"00101"},{"00110"},{"00111"},{"01000"},{"01001"},{"01010"},{"01011"},{"01100"},{"01101"},{"01110"},{"01111"},{"10000"},{"10001"},{"10010"},{"10011"},{"10100"},{"10101"},{"10110"},{"10111"},{"11000"},{"11001"},{"11010"}};
char ans[30][30];
char num[1000];
char s[100];
int main()
{
    int i,j,n,mcase;
    scanf("%d",&n);
    for(mcase=1;mcase<=n;++mcase)
    {
      for(i=0;i<30;++i)
        for(j=0;j<30;++j)
            ans[i][j]='*';

        int h,l;//行列
        scanf("%d%d",&h,&l);
        getchar();
        gets(s);
        i=0;
        memset(num, '\0', sizeof(num));
        while(s[i]!='\0')
        {
            if(s[i]==' ') strcat(num,a[0]);
            else
            {
                int x=s[i]-'A'+1;
                strcat(num,a[x]);
            }

            i++;
        }
        for (int i = 0; i < 400; i++) {
            strcat(num, "0");
        }

        int tot=0;
        int x=0,y=0;
        ans[0][0]=num[0];
        while(tot<h*l-1)
        {
            while(y+1<l && ans[x][y+1] == '*')
            {
                 ans[x][++y] = num[++tot];
            }

            while(x+1<h && ans[x+1][y] == '*')
            {
                ans[++x][y] = num[++tot];
            }

            while(y-1>=0 && ans[x][y-1] == '*')
            {
                ans[x][--y] = num[++tot];
            }

            while(x-1>=0 && ans[x-1][y] == '*')
            {
                ans[--x][y] = num[++tot];
            }

        }
        printf("%d ",mcase);
        for(i=0;i<h;++i)
            for(j=0;j<l;++j)
            {
                if(ans[i][j] == '*') printf("0");
                else printf("%c",ans[i][j]);
            }
        printf("\n");
}
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值