sicily 1036. Crypto Columns


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.
题解
0 1 2 3 4 5 /* 在keyword中的出现顺序 */
1 0 4 2 3 5 /* 在字母表中出现的顺序,重复的先标左边 */
B A T B O Y

将这个顺序(1 0 4 2 3 5)放在order数列里

klen = keyword的长度
将原字符串拆成klen行
0   EYDE
1   MBLR
2   THAN
3   MEKT
4   ETOE
5   EOTH

按照order里的顺序,将对应行塞进ans中
1   MBLR
0   EYDE
4   ETOE
2   THAN
3   MEKT
5   EOTH


竖着打印出来就是答案

AC代码

View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 char ans[12][150] = {0};
 4 const char end[] = "THEEND";
 5 int main()
 6 {
 7     int i, j, klen, tlen, clen, l;
 8     char key[12] = {0};
 9     char text[110] = {0};
10     int order[10];
11     while( gets(key) && strcmp(key, end) )
12     {
13         gets(text);
14         
15         memset( ans, 0, sizeof(ans) );
16         klen = strlen(key);
17         tlen = strlen(text);
18         clen = tlen/klen;
19         
20         l = 0;
21         for ( i = 0; i < 26; i++ )
22             for ( j = 0; j < klen; j++ )
23                 if( key[j] == 'A' + i )
24                     order[j] = l++;
25         
26         
27         for ( i = 0; i < klen; i++)
28             strncpy( ans[i], text+order[i]*clen, clen );
29         
30         for ( i = 0; i < clen; i++)
31             for ( j = 0; j < klen; j++)
32                 printf("%c", ans[j][i] );
33         
34         printf("\n");
35     }
36     
37     return 0;
38 }

长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn