HDU 1200 To and Fro

来源:互联网 发布:星知传媒 编辑:程序博客网 时间:2024/05/20 01:38

To and Fro

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6025    Accepted Submission(s): 4143


Problem Description
Mo and Larry have devised a way of encrypting messages. They first decide secretly on the number of columns and write the message (letters only) down the columns, padding with extra random letters so as to make a rectangular array of letters. For example, if the message is “There’s no place like home on a snowy night” and there are five columns, Mo would write down

t o i o y
h p k n n
e l e a i
r a h s g
e c o n h
s e m o t
n l e w x


Note that Mo includes only letters and writes them all in lower case. In this example, Mo used the character ‘x’ to pad the message out to make a rectangle, although he could have used any letter.

Mo then sends the message to Larry by writing the letters in each row, alternating left-to-right and right-to-left. So, the above would be encrypted as

toioynnkpheleaigshareconhtomesnlewx

Your job is to recover for Larry the original message (along with any extra padding letters) from the encrypted one.
 

Input
There will be multiple input sets. Input for each set will consist of two lines. The first line will contain an integer in the range 2. . . 20 indicating the number of columns used. The next line is a string of up to 200 lower case letters. The last input set is followed by a line containing a single 0, indicating end of input.
 

Output
Each input set should generate one line of output, giving the original plaintext message, with no spaces.
 

Sample Input
5toioynnkpheleaigshareconhtomesnlewx3ttyohhieneesiaabss0
 

Sample Output
theresnoplacelikehomeonasnowynightxthisistheeasyoneab
 
题目大意:
就是说Mo and Larry往来都是用加密的信写的,然后Mo喜欢把信中的单词排列呈矩形,然后从左边开始从上到下,就可以把这段话中的单词全找出来了,如果所给的单词不够凑成一个矩形,那么他就会在密文后面加x;现在我们要做的就是帮助 Larry还原Mo的密文;

分析:
这题有点像蛇形填空了,就是左右左右的模拟过去,首先呢我满判断他该字符串的长度,然后除以所给的宽度,看能够得到多少行;
然后呢,只要该行为偶数行,我们就顺着放,也就是从左到右依次放就好了;
如果为单数行,那么我们就要倒着放,也就是从右到左;
最后遍历的时候,我们需要从左边第一个开始,从上到下的遍历过去,就能到到解密的信了;

给出AC代码:
#include<iostream>#include<cstring>using namespace std;int main(){char str[1005][25], key[100005];int c;while (cin >> c, c){getchar();gets(key);int len = strlen(key);int r = 0;for (int i = 0; i < len; i++){if ((r+1)%2!=0){for (int j = 0; j < c; j++,i++){str[r][j] = key[i];}i = i - 1;r++;}else {for (int j = c - 1; j >= 0; j--,i++){str[r][j] = key[i];}r++;i = i - 1;}}for (int j = 0; j < c;j++)for (int i = 0; i < r; i++)cout << str[i][j];cout << endl;}return 0;}


0 0
原创粉丝点击