码迷,mamicode.com
首页 > 其他好文 > 详细

poj1026

时间:2014-05-12 12:18:48      阅读:371      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   tar   

Cipher
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18970   Accepted: 5067

Description

Bob and Alice started to use a brand-new encoding scheme. Surprisingly it is not a Public Key Cryptosystem, but their encoding and decoding is based on secret keys. They chose the secret key at their last meeting in Philadelphia on February 16th, 1996. They chose as a secret key a sequence of n distinct integers, a1 ; . . .; an, greater than zero and less or equal to n. The encoding is based on the following principle. The message is written down below the key, so that characters in the message and numbers in the key are correspondingly aligned. Character in the message at the position i is written in the encoded message at the position ai, where ai is the corresponding number in the key. And then the encoded message is encoded in the same way. This process is repeated k times. After kth encoding they exchange their message. 

The length of the message is always less or equal than n. If the message is shorter than n, then spaces are added to the end of the message to get the message with the length n. 

Help Alice and Bob and write program which reads the key and then a sequence of pairs consisting of k and message to be encoded k times and produces a list of encoded messages. 

Input

The input file consists of several blocks. Each block has a number 0 < n <= 200 in the first line. The next line contains a sequence of n numbers pairwise distinct and each greater than zero and less or equal than n. Next lines contain integer number k and one message of ascii characters separated by one space. The lines are ended with eol, this eol does not belong to the message. The block ends with the separate line with the number 0. After the last block there is in separate line the number 0.

Output

Output is divided into blocks corresponding to the input blocks. Each block contains the encoded input messages in the same order as in input file. Each encoded message in the output file has the lenght n. After each block there is one empty line.

Sample Input

10
4 5 3 7 2 8 1 6 10 9
1 Hello Bob
1995 CERC
0
0

Sample Output

BolHeol  b
C RCE

求置换的循环节
bubuko.com,布布扣
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #define MAX 202
 5 int key[MAX], t[MAX];
 6 void get_time(int n)
 7 {
 8     int i, j, count;
 9     for (i = 1; i <= n; i++)
10     {
11         j = key[i];
12         count = 1;
13         while(i != j)
14         {
15             count++;
16             j = key[j];
17         }
18         t[i] = count;
19     }
20 }
21 int main()
22 {
23     int i, j, k, n, len;
24     char src[MAX], dst[MAX];
25 
26     while(scanf("%d", &n)&&n)
27     {
28         for (i = 1; i <= n; i++)
29             scanf("%d", &key[i]);
30         memset(t, 0, sizeof(t));
31         get_time(n);
32         while(scanf("%d", &k)&&k)
33         {
34             getchar();
35             gets(src + 1);
36             for (i = strlen(src + 1) + 1; i <= n; i++)
37                 src[i] =  ;
38             src[n + 1] = 0;
39             for (i = 1; i <= n; i++)
40             {
41                 int pos = i;
42                 for (j = 0; j < k % t[i]; j++)
43                     pos = key[pos];
44                 dst[pos] = src[i];
45             }
46             dst[n + 1] = 0;
47             puts(dst + 1);
48         }
49         printf("\n");
50     }
51     return 0;
52 }
View Code

 

poj1026,布布扣,bubuko.com

poj1026

标签:des   style   blog   class   code   tar   

原文地址:http://www.cnblogs.com/lxm940130740/p/3720328.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!