sicily 1007 to and from 密文解码 数组与下标

1007. To and Fro

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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

5
toioynnkpheleaigshareconhtomesnlewx
3
ttyohhieneesiaabss
0

Sample Output

theresnoplacelikehomeonasnowynightx
thisistheeasyoneab
#include <iostream>
using namespace std;

//生成密文用,此处无用
void filter(string &str) {
	string temp;
	for (int i = 0; i < str.length(); i++) {
		if (str[i] >= 97 && str[i] <= 122) {
			temp += str[i];
		} else if (str[i] >= 65 && str[i] <= 90) {
			temp += (str[i] + 32);
		}
	}
	str = temp;
} 

int main() {
	int n;
	string str;
	int col_capacity; 

	while (cin >> n && n) {

		string sub_str[n]; //存放密文分解出来的字符串,一共n列
		cin >> str;

		//确定每列的字符数
		if (str.length() % n != 0) {
			col_capacity = str.length() / n + 1; 	

		} else {
			col_capacity = str.length() / n;
		}

		int k = 0; //用于遍历密文

		for (int i = 0; i < col_capacity; i++) { //依次填充每列从上到下的字符,构成一行 

			if (i % 2 == 0) { // 偶数行则从左到右填充
				for (int j = 0; j < n; j++)
					sub_str[j].push_back(str[k++]);
			} else { //奇数行则从右到左填充
				for (int j = n-1; j >= 0; j--) {
					sub_str[j].push_back(str[k++]);
				}
			}
		}
		//依次输出各列即得到明文
		for (int i = 0; i < n; i++) {
			cout << sub_str[i];
		}
		cout << endl;
	}
	return 0;
}

  

时间: 2024-10-15 08:50:19

sicily 1007 to and from 密文解码 数组与下标的相关文章

sicily 1036 字符串解码 数组与下标解题

1036. Crypto Columns Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description The columnar encryption scheme scrambles the letters in a message (or plaintext) using a keyword as illustrated in the following example: Suppose BATBOY is the keywo

编程题:二维数组的下标意义

#include<stdio.h> void main() { int i,j,a[2][3],b[2][3]; for(i=0;i<2;i++) for(j=0;j<3;j++) a[i][j]=i; for(i=0;i<2;i++) for(j=0;j<3;j++) b[i][j]=j; printf("array a:\n"); for(i=0;i<2;i++) { for(j=0;j<3;j++) printf("%3d

PHP 获取数组任意下标key的上一个prev和下一个next下标值

PHP 获取数组任意下标key的上一个prev和下一个next下标值 <?php $xoops[1] = '小'; $xoops[2] = '孩'; $xoops[3] = '子'; $xoops[4] = '气'; $steps = new Steps(); foreach($xoops as $key=>$value){ $steps->add($key); } $steps->setCurrent(3);//参数为key值 echo '上一个下标:'.$steps->g

js数组的用法以及数组根据下标(数值或字符)移除元素

1.创建数组 var array = new Array(); var array = new Array(size);//指定数组的长度 var array = new Array(item1,item2……itemN);//创建数组并赋值 2.取值.赋值 var item = array[index];//获取指定元素的值 array[index] = value;//为指定元素赋值 3.添加新元素 array.push(item1,item2……itemN);//将一个或多个元素加入数组,

C语言数组元素下标为何从0开始

很多同学可能在学习数组时会有这个疑问,下标为什么不从1开始呢?从1开始不是更符合大家的日常习惯吗?生活中我们通常说第1个,而不是第0个.的确,有些计算机语言如早期的Pascal语言,数组元素的下标是从1开始的.难道是C语言故意要与众不同?要弄清楚这个问题,得先看一下计算机底层是怎样处理数组元素的.我们先编写了一个小程序,然后在visual studio中对其进行了反汇编.源程序和反汇编后的部分代码如下: 源程序: int arr[5];  //一个全局数组 int main() { int i;

稀疏矩阵某个元素对应一维数组的下标

稀疏矩阵:矩阵中大量元素都是零 存储系数矩阵通常只存上三角或下三角 另一半的信息由已知信息推断 一,上三角a[4][4]               上三角对应的以为数组的下标:(2n-i+1)*i/2+j 二,下三角                   上三角对应的以为数组的下标:(i+1)*i/2+j 原文地址:https://www.cnblogs.com/excellencesy/p/8647473.html

sicily 1136(线段树+最大子数组)

题目链接:sicily 1136 解题思路: 要求区间内的最大子数组,而且访问可能很频繁,时间复杂度需要达到o(n*logn),于是就很自然地想到了线段树.我们可以用线段树来保存区间的最大子数组,但是仔细想想又不对劲了,如果访问的区间跨越了两个小区间怎么破,所以,这并不只是一个简单的求区间连续和的问题,还要有点小技巧. 最大子数组怎么得到的,还记得<算法导论>里面讲过一种用分治法来求最大子数组的方法吗(分治法之最大子数组)? 假设我们要求区间[low , high]的最大子数组,并且已知[lo

sicily 1007 To and Fro (基础题)

链接:http://soj.me/show_problem.php?pid=1007 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 letter

js数组依据下标删除元素

最近在项目中遇到了一些问题,基础性的东西记得不牢固,就总结一下放在这里备再次查找,对操作js数组的一些问题一些常用的记录! 1.创建数组 var array = new Array(); var array = new Array(size);//指定数组的长度 var array = new Array(item1,item2……itemN);//创建数组并赋值 2.取值.赋值 var item = array[index];//获取指定元素的值 array[index] = value;//