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 keyword and our message is MEET ME BY THE OLD OAK TREE. Since the keyword has 6 letters, we write the message (ignoring spacing and punctuation) in a grid with 6 columns, padding with random extra letters as needed:

MEETME
BYTHEO
LDOAKT
REENTH

Here, we‘ve padded the message with NTH. Now the message is printed out by columns, but the columns are printed in the order determined by the letters in the keyword. Since A is the letter of the keyword that comes first in the alphabet, column 2 is printed first. The next letter, B, occurs twice. In the case of a tie like this we print the columns leftmost first, so we print column 1, then column 4. This continues, printing the remaining columns in order 5, 3 and finally 6. So, the order the columns of the grid are printed would be 2, 1, 4, 5, 3, 6, in this case. This output is called the ciphertext, which in this example would be EYDEMBLRTHANMEKTETOEEOTH. Your job will be to recover the plaintext when given the keyword and the ciphertext.

Input

There will be multiple input sets. Each set will be 2 input lines. The first input line will hold the keyword, which will be no longer than 10 characters and will consist of all uppercase letters. The second line will be the ciphertext, which will be no longer than 100 characters and will consist of all uppercase letters. The keyword THEEND indicates end of input, in which case there will be no ciphertext to follow.

Output

For each input set, output one line that contains the plaintext (with any characters that were added for padding). This line should contain no spacing and should be all uppercase letters.

Sample Input

BATBOY
EYDEMBLRTHANMEKTETOEEOTH
HUMDING
EIAAHEBXOIFWEHRXONNAALRSUMNREDEXCTLFTVEXPEDARTAXNAARYIEX
THEEND

Sample Output

MEETMEBYTHEOLDOAKTREENTH
ONCEUPONATIMEINALANDFARFARAWAYTHERELIVEDTHREEBEARSXXXXXX
#include <iostream>
#include <algorithm>
#include <map>
#include <cstring>
using namespace std;
bool cmp (const char a, const char b) {
	return a < b;
}
int main() {
	string keyword;
	string ciphertext;

	while (cin >> keyword && keyword.compare("THEEND") != 0) {
		cin >> ciphertext;

		map<int, char> key_order;
		for (int i = 0; i < keyword.length(); i++) {
			key_order[i] = keyword[i]; //i代表keyword中的该字符对应的子串应该位于第几列
		}

		sort(keyword.begin(), keyword.end(), cmp);//对keyword进行升序排序,使得每个字符依次与输入的字符串的子串对应 

		string sub_strs[keyword.length()]; //字符列
		int sub_s = 0; //每列字符串的开始坐标
		int sub_e = ciphertext.length() / keyword.length(); //每列字符串的结束坐标
		bool visited[keyword.length()]; //visited用于记录已经访问过得字符,避免出现如ABBCD这样每次都访问到第一个B的情况
		memset(visited, false, sizeof(visited));

		for (int i = 0; i < keyword.length(); i++) { //遍历排序后的keyword
		    //对每个字符扫描key_order来决定把keyword中的该字符放在哪个列中
			for (int j = 0; j < keyword.length(); j++) { //根据key_order觉得把对应的子串放到哪列中,即j列
				if (key_order[j] == keyword[i] && visited[j] == false) {
					sub_strs[j] = ciphertext.substr(sub_s, sub_e);
					sub_s = sub_e;
					sub_e += ciphertext.length() / keyword.length();
					visited[j] = true;
					break;
				}
			}
		}
		//从左到右,从上到下依次输出
		for (int i = 0; i < ciphertext.length() / keyword.length(); i++) {
			for (int j = 0; j < keyword.length(); j++)
			cout << sub_strs[j].at(i);
		}
		cout << endl;
	}
	return 0;
}

  

时间: 2024-11-14 23:26:11

sicily 1036 字符串解码 数组与下标解题的相关文章

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, paddin

字符串到字节数组和字节数组到字符串的转换(编码和解码问题)

/* * String(byte[] bytes, String charsetName):通过指定的字符集解码字节数组 * byte[] getBytes(String charsetName):使用指定的字符集合把字符串编码为字节数组 * * 编码:把看得懂的变成看不懂的 * String -- byte[] * * 解码:把看不懂的变成看得懂的 * byte[] -- String * * 举例:谍战片(发电报,接电报) * * 码表:小本子 *         字符    数值 * *

js字符串、数组、时间、日期对象

js对字符串.数组.日期的操作是在以后项目中频繁使用的操作,所以呢....所以大家看着办,其实并不难哈,就是有点无聊,我承认这是我百度的,哈哈哈哈 <!DOCTYPE html><html><head><meta charset="UTF-8"><title>JavaScript字符串对象</title></head><body><script type="text/java

golang 数据一 &nbsp; (字符串、数组和数组指针)

从如下几个方面介绍GO语言的数据 1. 字符串 2. 数组 3. 切片 4. 字典 5. 结构 字符串 Go语言中的字符串是由一组不可变的字节(byte)序列组成从源码文件中看出其本身是一个复合结构 string.go  type stringStruct struct {     str unsafe.Pointer         len int } 字符串中的每个字节都是以UTF-8编码存储的Unicode字符字符串的头部指针指向字节数组的开始但是没有NULL或'\0'结尾标志. 表示方式

Js中常用的字符串,数组,函数扩展

由于最近辞职在家,自己的时间相对多一点.所以就根据prototytpeJS的API,结合自己正在看的司徒大神的<javascript框架设计>,整理了下Js中常用一些字符串,数组,函数扩展,一来可以练练手,二来也锻炼下自己的代码能力.由于代码里面的注释自认为已经非常详细,所以就直接贴代码了. 1. 字符串扩展: ;(function() { var method, stringExtends = { /** * 删除字符串开始和结尾的空白 * @returns {string} */ stri

黑马程序员-OC加强学习日记-Foundation框架中的字符串和数组

------- ios培训. android培训.java培训.期待与您交流! ---------- 一.Foundation框架 1.概念:框架是由许多类.方法.函数.文档按照一定的逻辑组织起来的集合,以便使研发程序变得更容易. 为所有程序开发奠定基础的框架称为Foundation框架 2.使用:要想使用Foundation框架中的功能,包含它的主文件即可 #import <Foundation/Foundation.h> 二.NSString: 不可变字符串类   一个NSString对象

shell入门笔记2:字符串、数组、echo与printf

说明: 本文是关于http://c.biancheng.net/cpp/shell/的相关笔记 shell字符串 字符串可以用单引号,也可以用双引号,也可以不用引号. 1 #!/bin/bash 2 3 ##字符串 4 5 #不加引号: 6 #无法出现; 7 echo a 8 echo a;b 9 10 #单引号: 11 #单引号里的任何字符都会原样输出,单引号字符串中的变量替换是无效的: 12 #单引号字符串中不能出现单引号(对单引号使用转义符后也不行). 13 str='this is a

[面试没有回答上的问题4]常用字符串和数组的操作。

面试的时候有问到我如果用空格分割一个字符串,其实是一个很简单的问题,但是自己突然忘记了,这里做一个对字符串和数组操作的常用方法的一个简单的复习. 字符串部分 1.split(); split() 方法用于把一个字符串分割成字符串数组. 语法 : stringObject.split(separator, howmany) 参数 : separator 必需.字符串或正则表达式,从该参数指定的地方分割 stringObject.如果为("")空字符串,就表示字符串中每个字符都会被分割.

shell 之解释器、变量、字符串、数组

1.Shell简介 Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言.Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务.Shell 编程跟 java.php 编程一样,只要有一个能编写代码的文本编辑器和一个能解释执行的脚本解释器就可以了常见的有: Bourne Shell(/usr/bin/sh或/bin/sh) Bourne Again Shell(/bin/bash)