TOJ 3007. Decoding

来源:互联网 发布:c语言区分大小写吗 编辑:程序博客网 时间:2024/05/03 03:23

终于AC了!!!!!!!!!!!!一直是Presentation Error,知道是末尾不能有空格的原因,但是就是改不好,而且不能用while循环来判断,因为会超时,我的方法是这样的:

用一个变量fla代表出现的空格数,初始化为0;只要遇到是空格的情况先不要急着输出,而是累加fla,在下一个非空格的字符输出前输出,而且fla还累计空格个数。

我能够说我提交了20多遍都是错误的吗?

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 string of binary digits that represents the contents of the matrix (R * C binary digits). The binary digits are inrow major order.

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 thedecoded text message. You should throw away any trailing spaces and/or partial characters found while decoding.

Sample Input

44 4 00001101001011005 2 01100000102 6 0100000010015 5 0100001000011010110000010

Sample Output

1 ACM2 HI3 HI4 HI HO
AC代码:

<pre name="code" class="cpp">
<span style="font-size:10px;"></span><pre name="code" class="cpp">#include<iostream>#include<string>using namespace std;int main(){int N,r,c,l,i,j,k,a[21][21],b[410],flag[21][21],m;char t;cin>>N;for(l=1;l<=N;l++){cin>>r>>c;for(i=0;i<r;i++)for(j=0;j<c;j++){cin>>t;a[i][j]=t-'0';flag[i][j]=0;}flag[0][0]=1;m=r*c;k=0;i=0;j=0;int f=1;//1,2,3,4分别代表向右,下,左,上while(true){b[k]=a[i][j];k++;if(k==m)break;if(f==1){if(j==c-1||flag[i][j+1]==1){f=2;i++;}else j++;}else if(f==2){if(i==r-1||flag[i+1][j]==1){f=3;j--;}else i++;}else if(f==3){if(j==0||flag[i][j-1]==1){f=4;i--;}else j--;}else if(f==4){if(i==0||flag[i-1][j]==1){f=1;j++;}else i--;}flag[i][j]=1;}cout<<l<<" ";int fla=0;for(i=0;i<=m-5;i=i+5){k=b[i];for(j=i+1;j<i+5;j++){k=k*2;k=k+b[j];}if(k==0){fla++;}else{if(fla!=0){for(int kk=0;kk<fla;kk++)cout<<" ";fla=0;}char s=k+'A'-1;cout<<s;}}cout<<endl;}return 0;}




0 0