SOJ 1036

来源:互联网 发布:mysql默认配置文件 编辑:程序博客网 时间:2024/06/06 07:29

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

BATBOYEYDEMBLRTHANMEKTETOEEOTHHUMDINGEIAAHEBXOIFWEHRXONNAALRSUMNREDEXCTLFTVEXPEDARTAXNAARYIEXTHEEND

Sample Output

MEETMEBYTHEOLDOAKTREENTHONCEUPONATIMEINALANDFARFARAWAYTHERELIVEDTHREEBEARSXXXXXX
  很简单的题目,依照题意进行模拟还原即可。
// Problem#: 1036// Submission#: 5054968// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include<vector>#include<algorithm>#include<iostream>#include<string>using namespace std;struct node{//UPPER保存keyword的一个字母,num保存这一字母在keyword中的序号     char UPPER;    int num;    node(char c,int n)    {        UPPER=c;        num=n;    }};vector<node> keyword;//将keyword转换成node节点存入容器 char plain[101][101];//用二维数组来保存原文 string cipher,key;//cipher保存密文字符串,key保存keyword字符串 bool cmp(node A,node B)//对keyword的字母进行稳定排序 {    if(A.UPPER!=B.UPPER) return A.UPPER<B.UPPER;    else return A.num<B.num;}int main(){    while(cin>>key&&key!="THEEND")    {        keyword.clear();        cin>>cipher;        for(int i=0;i<key.size();++i)        {            keyword.push_back(node(key[i],i+1));        }        sort(keyword.begin(),keyword.end(),cmp);//排序后keyword中的字母按字典序排列且相同字母的先后顺序不变         for(int i=0;i<key.size();++i)        {             for(int j=1;j<=cipher.size()/key.size();++j)//每列的字符数量为 cipher.size()/key.size()             {                plain[j][keyword[i].num]=cipher[i*cipher.size()/key.size()+j-1];//将每列字符放回原来位置              }        }        for(int i=1;i<=cipher.size()/key.size();++i)//恢复原状后再按先左右后上下的顺序打印就是原文         {            for(int j=1;j<=key.size();++j)            {                cout<<plain[i][j];            }        }        cout<<endl;    }    return 0;}                                 

0 0