sicily 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 <string>
#include <map>
#include <vector>
using namespace std;

int main(int argc, char *argv[])
{
    string keyWorld;
    string message;

    while (cin >> keyWorld && keyWorld != "THEEND") {
        cin >> message;

        multimap<char, int> keyOrder;
        int eachRowNum = keyWorld.size();

        for (int i = 0; i != eachRowNum; ++i) {
            keyOrder.insert(pair<char, int>(keyWorld[i], i));
        }

        int colNum = message.size() / eachRowNum;

        vector<string> result;
        for (int i = 0; i != eachRowNum; i++) {
            result.push_back(message.substr(i * colNum, colNum));
        }
        int index = 0;
        int re[10];
        for (multimap<char, int>::iterator iter = keyOrder.begin();
                                    iter != keyOrder.end(); ++iter) {
                re[iter->second] = index++;
        }

        for (int i = 0; i != colNum; ++i) {
            for (int j = 0; j != eachRowNum; ++j) {
                cout << result[re[j]][i];
            }
        }
        cout << endl;
    }

    return 0;
}
时间: 2024-07-29 05:26:05

sicily 1036. Crypto Columns的相关文章

SOJ 1036. Crypto Columns

题目大意:一个字符串,去掉空格和符号后变成"plaintext".另一个字符串叫"keyword".将plaintext排成多行,每行有keyword.size的宽度,不能排满一列的用别的字符填满,生成了字符矩阵.每次选取keyword中字典序最小的字符对应的列,作为需要选取的矩阵的列,被选择过的字符不再被选择,最后形成一行新的字符串,作为输出字符串.解题思路:对keyword中的字符及其对应的列进行排序,利用排序结果还原成字符矩阵,然后按行输出字符矩阵.代码如下:

1036. Crypto Columns 2016 11 02

/* 对于题目多读几遍,然后再关键字排序的时候,把对应的数组序号也排序, EYDE    MBLR    THAN    MEKT    ETOE    EOTH        MEETME    BYTHEO    LDOAKT    REENTH */ #include<iostream>#include<string>#include<algorithm>using namespace std;int main(){ int n; string keyword; 

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

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

HDU——PKU题目分类

HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201

(转载)ACM训练计划,先过一遍基础再按此拼搏吧!!!!

ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. USACO http://ace.delos.com/usacogate 美国著名在线题库,专门为信息学竞赛选手准备 TJU http://acm.tongji.edu.cn/ 同济大学在线题库,唯一的中文题库,适合NOIP选手 ZJU http://acm.zju.edu.cn/ 浙江大学在线题库 JLU htt

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

北大ACM题库习题分类与简介(转载)

在百度文库上找到的,不知是哪位大牛整理的,真的很不错! zz题 目分类 Posted by fishhead at 2007-01-13 12:44:58.0 -------------------------------------------------------------------------------- acm.pku.edu.cn 1. 排序 1423, 1694, 1723, 1727, 1763, 1788, 1828, 1838, 1840, 2201, 2376, 23

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京