2013
12-26

# Encoding

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

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.

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.

4
4 4 ACM
5 2 HI
2 6 HI
5 5 HI HO

1 0000110100101100
2 0110000010
3 010000001001
4 0100001000011010110000010

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int main()
{
char str[10000];
int len,i,j,cnt,s,e;
while(gets(str))
{
len = strlen(str);
i = s = e = 0;
while(1)
{
cnt = 1;
while(str[i] == str[i+cnt] && cnt<10 && i<len)
cnt++;
if(i == len)
{
if(e)
printf("1");
break;
}
else if(cnt!=1)
{
s = 0;
if(e)
{
printf("1");
e = 0;
}
if(cnt<10)
printf("%d%c",cnt,str[i]);
else
printf("%d%c",--cnt,str[i]);
}
else
{
e = 1;
if(!s)
{
printf("1");
s = 1;
}
printf("%c",str[i]);
if(str[i] == '1')
printf("%c",str[i]);
}
i+=cnt;
}
printf("\n");
}

return 0;
}

1. 约瑟夫也用说这么长……很成熟的一个问题了，分治的方法解起来o(n)就可以了，有兴趣可以看看具体数学的第一章，关于约瑟夫问题推导出了一系列的结论，很漂亮

2. 约瑟夫也用说这么长……很成熟的一个问题了，分治的方法解起来o(n)就可以了，有兴趣可以看看具体数学的第一章，关于约瑟夫问题推导出了一系列的结论，很漂亮

3. #include <cstdio>
#include <cstring>

const int MAXSIZE=256;
//char store[MAXSIZE];
char str1[MAXSIZE];
/*
void init(char *store) {
int i;
store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
for(i=’F';i<=’Z';++i) store =i-5;
}
*/
int main() {
//freopen("input.txt","r",stdin);
//init(store);
char *p;
while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
if(p=fgets(str1,MAXSIZE,stdin)) {
for(;*p;++p) {
//*p=store[*p]
if(*p<’A’ || *p>’Z') continue;
if(*p>’E') *p=*p-5;
else *p=*p+21;
}
printf("%s",str1);
}
fgets(str1,MAXSIZE,stdin);
}
return 0;
}

4. 第2题，TCP不支持多播，多播和广播仅应用于UDP。所以B选项是不对的。第2题，TCP不支持多播，多播和广播仅应用于UDP。所以B选项是不对的。